Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

javascript - 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");

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


Maybe an example will help. Say the current page is http://www.example.com/dir/foo.html.

  • window.open("popup.html", "_blank") opens
    http://www.example.com/dir/popup.html.
  • window.open("www.google.com", "_blank") therefore opens
    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...