简体   繁体   English

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

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

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?我究竟做错了什么?

You wanted to access the root document of server www.google.com , which is done using the url https://www.google.com/ .您想访问服务器www.google.com的根文档,这是使用 URL https://www.google.com/完成的。 You provided a relative url for document www.google.com instead.您提供了文档www.google.com的相对 URL。

Keep in mind that window.open accepts both relative and absolute urls, so it can't assume you left out https:// as it does when you use www.google.com in the address bar.请记住, window.open接受相对网址和绝对网址,因此它不能假设您像在地址栏中使用www.google.com时那样遗漏了https://


Maybe an example will help.也许一个例子会有所帮助。 Say the current page is http://www.example.com/dir/foo.html .假设当前页面是http://www.example.com/dir/foo.html

  • window.open("popup.html", "_blank") opens window.open("popup.html", "_blank")打开
    http://www.example.com/dir/popup.html . http://www.example.com/dir/popup.html
  • window.open("www.google.com", "_blank") therefore opens window.open("www.google.com", "_blank")因此打开
    http://www.example.com/dir/www.google.com . http://www.example.com/dir/www.google.com

The browser has no way of knowing you actually wanted https://www.google.com/ when you said you wanted http://www.example.com/dir/www.google.com since the latter could be valid.当您说您想要http://www.example.com/dir/www.google.com时,浏览器无法知道您实际上想要https://www.google.com/ ,因为后者可能是有效的。

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/

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.更多信息:如果您事先包含一个“/”,它会将您的字符串附加到根 URL。 If you just list the string, it appends it to the current full URL.如果您只是列出字符串,它会将其附加到当前的完整 URL。 If you include either http:// or https:// it knows to use only what you put in your string (ie http://www.google.com )如果您包含 http:// 或 https:// ,它知道只使用您放入字符串中的内容(即http://www.google.com

http://开头您的网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM