javascript window.open() 只是将 url 添加到我当前的 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23373990/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:46:40  来源:igfitidea点击:

window.open() simply adds the url to my current url

javascriptjquery

提问by

When I use window.open("www.google.com", "_blank");

当我使用 window.open("www.google.com", "_blank");

window.open("www.google.com", "_blank");

A new tab is opened, but the url isn't "www.google.com", it's "=url-i-was-at=/www.google.com".

打开一个新选项卡,但网址不是“www.google.com”,而是“=url-i-was-at=/www.google.com”。

This is a snippet of the code (and the only relevant code). http://jsfiddle.net/FUYTY/

这是代码片段(也是唯一相关的代码)。 http://jsfiddle.net/FUYTY/

In jsfiddle it behaves a bit differently, but still doesn't work as it should.

在 jsfiddle 中,它的行为有点不同,但仍然无法正常工作。

What am i doing wrong?

我究竟做错了什么?

采纳答案by ikegami

You wanted to access the root document of server www.google.com, which is done using the url http://www.google.com/. You provided a relative url for document www.google.cominstead.

您想访问服务器的根文档www.google.com,这是使用 url 完成的http://www.google.com/。您为文档提供了一个相对 url www.google.com

回答by amatellanes

You have to prepend http://in your url:

你必须http://在你的网址前加上:

$(document).ready(function () {
    $('#mybtn').on('click', function () {
        window.open("http://www.google.com", '_blank');
    });
});

Fix: http://jsfiddle.net/FUYTY/4/

修复:http: //jsfiddle.net/FUYTY/4/

回答by Laura Ritchey

Try adding http:// beforehand (see Fiddle http://jsfiddle.net/lkritchey/FUYTY/3/)

尝试预先添加 http://(参见 Fiddle http://jsfiddle.net/lkritchey/FUYTY/3/

$( document ).ready(function() {
  $('#mybtn').on('click', function() {
      window.open("http://www.google.com", '_blank');   
  });
});

Some more information: If you include a '/' beforehand, it appends your string to the root URL. If you just list the string, it appends it to the current full URL. If you include either http:// or https:// it knows to use only what you put in your string (i.e. http://www.google.com)

更多信息:如果您事先包含“/”,它会将您的字符串附加到根 URL。如果您只列出字符串,它会将其附加到当前的完整 URL。如果您包含 http:// 或 https:// 它知道只使用您在字符串中放入的内容(即http://www.google.com

回答by Seth T

Preface your urls with http://

在你的网址前加上 http://