没有http://的javascript window.open [英] javascript window.open without http://

查看:112
本文介绍了没有http://的javascript window.open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Delphi构建的小工具,它可以从文件或剪贴板中收集URL,然后构建一个名为test.htm的文件,其内容如下:

I have a small tool build with Delphi that collects url's from a file or from the clipboard, and than builds a file called test.htm with a content like this :

<!DOCTYPE html> 
<html> 
<body> 
    <p>Click the button retrieve the links....</p> 
    <button onclick="myFunction()">Click me</button> 
    <p id="demo"></p> 
    <script> 
        function myFunction() { 
            window.open('http://www.speedtest.net/', '_blank');
            window.open('www.speedtest.net/', '_blank');
            and so on...
        }
    </script> 
</body>
</html>

想法是单击按钮,然后为myFunction中的每个URL创建一个新的选项卡(或窗口).这可行,但是有一个小问题.

The idea is to click on the button, and then a new tab (or window) is created for every url inside myFunction. This works, but with one small problem.

在代码示例中,有2个url,一个带有http://前缀,另一个不带url.第一个网址可以正常工作,并使用以下网址创建一个新的标签页(或窗口):

In the code example there are 2 url's, one with the http:// prefix and one without it. The first url works as expected and creates a new tab (or window) with the following url:

http://www.speedtest.net

第二个"window.open"无法正常运行.此"window.open"将在新标签页(或窗口)中创建以下网址

The second 'window.open' does not work as I expected. This 'window.open' will create the following url in the new tab (or window)

file:///c:/myApplicaton/www.speedtest.net

您已经知道,该应用程序是c:\ myApplication中的可执行文件

As you have already figured out, the application is an executable in c:\myApplication

所以我的问题是,有没有一种方法可以使用"window.open"创建一个新的选项卡(或窗口),而无需将应用程序的路径放在url的前面?如果使用"window.open"无法做到这一点,还有另一种方法吗?

So my question(s) is, is there a way to use 'window.open' to create a new tab (or window) without putting the path of the application in front of the url ? If this is not possible with 'window.open', is there another way to do this ?

或者是使应用程序将http://放在尚不存在的每个URL前面的唯一方法吗?

Or is the only way to do this to have the application put the http:// in front of every url that does not have it already ?

推荐答案

如您所建议,唯一的方法是将http协议添加到每个缺少它的URL.这是一个非常简单明了的解决方案,并具有其他好处.

As you suggested, the only way is to add the http protocol to each URL which is missing it. It's a pretty simple and straightforward solution with other benefits to it.

考虑这段代码:

function windowOpen(url, name, specs) {
    if (!url.match(/^https?:\/\//i)) {
        url = 'http://' + url;
    }
    return window.open(url, name, specs);
}

我通常要做的是还添加将规范作为对象传递的功能,在我看来,它比字符串更易于管理,甚至在需要时设置规范默认值,您还可以自动创建和创建名称.将该参数设为可选,以防对您的原因造成不必要的影响.

What I usually do is to also add the functionality of passing specs as an object, which is much more manageable, in my opinion, than a string, even setting specs defaults if needed, and you can also automate the name creation and make the argument optional in case it's redundant to your cause.

这是该函数下一阶段的外观示例.

Here's an example of how the next stage of this function may look like.

function windowOpen(url, name, specs) {
    if (!url.match(/^https?:\/\//i)) {
        url = 'http://' + url;
    }
    // name is optional
    if (typeof name === 'object') {
        specs = name;
        name = null;
    }
    if (!name) {
        name = 'window_' + Math.random();
    }
    if (typeof specs === 'object') {
        for (var specs_keys = Object.keys(specs), i = 0, specs_array = [];
                i < specs_keys.length; i++) {
            specs_array.push(specs_keys[i] + '=' + specs[specs_keys[i]]);
        }
        specs = specs_array.join(',');
    }
    return window.open(url, name, specs);
}

这篇关于没有http://的javascript window.open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆