javascript window.open 没有 http://
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29684740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript window.open without http://
提问by GuidoG
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 :
我有一个用 Delphi 构建的小工具,它从文件或剪贴板中收集 url,然后构建一个名为 test.htm 的文件,其内容如下:
<!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>
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.
这个想法是单击按钮,然后为 myFunction 中的每个 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:
在代码示例中有 2 个 url,一个带有 http:// 前缀,一个没有它。第一个 url 按预期工作,并使用以下 url 创建一个新选项卡(或窗口):
http://www.speedtest.net
The second 'window.open' does not work as I expected. This 'window.open' will create the following url in the new tab (or window)
第二个“window.open”没有按我的预期工作。此“window.open”将在新选项卡(或窗口)中创建以下网址
file:///c:/myApplicaton/www.speedtest.net
As you have already figured out, the application is an executable in c:\myApplication
正如您已经发现的,该应用程序是 c:\myApplication 中的可执行文件
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 ?
所以我的问题是,有没有办法使用“window.open”来创建一个新选项卡(或窗口),而无需将应用程序的路径放在 url 前面?如果使用“window.open”无法做到这一点,还有其他方法可以做到这一点吗?
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 前面的唯一方法?
回答by iMoses
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.
正如您所建议的,唯一的方法是将 http 协议添加到每个缺少它的 URL 中。这是一个非常简单和直接的解决方案,还有其他好处。
Consider this piece of code:
考虑这段代码:
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);
}
回答by GolezTrol
The only way to do this is to have the application put the http://
in front of every url that does not have it already.
做到这一点的唯一方法是让应用程序将 放在http://
每个还没有它的 url 前面。
回答by sergioneli
I think the best way would be to add "//" + url In this case - it isn't important, what protocol (http or https) you expect to receive as a result.
我认为最好的方法是添加“//”+ url 在这种情况下 - 您期望收到的协议(http 或 https)并不重要。
url = url.match(/^https?:/) ? url : '//' + url;
window.open(url, '_blank');
回答by Hypaethral
For the behavior you're describing, you have to include your protocol with window.open. You could use a tertiary operator to simply include the protocol if it doesn't already exist:
对于您所描述的行为,您必须在 window.open 中包含您的协议。如果协议尚不存在,您可以使用三级运算符来简单地包含该协议:
url = url.match(/^http[s]?:\/\//) ? url : 'http://' + url;
Note that you'll need to use the SSL protocol sometimes, so this is not a complete solution.
请注意,有时您需要使用 SSL 协议,因此这不是一个完整的解决方案。
回答by Satish
I made small changes function form answered by iMoseswhich worked for me.
Check for both https OR http protocol
检查 https 或 http 协议
if (!url.match(/^http?:\/\//i) || !url.match(/^https?:\/\//i)) {
url = 'http://' + url;
}
Hope it make more accurate for other situation !
希望它对其他情况更准确!