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

2020-02-05 11:31发布

When I use 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".

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

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

What am i doing wrong?

4条回答
家丑人穷心不美
2楼-- · 2020-02-05 12:19

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

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

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

查看更多
淡お忘
3楼-- · 2020-02-05 12:19

Preface your urls with http://

查看更多
做个烂人
4楼-- · 2020-02-05 12:27

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.com instead.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-02-05 12:32

Try adding http:// beforehand (see 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)

查看更多
登录 后发表回答