elseytd elseytd - 6 months ago 32
Javascript Question

Javascript window.open() not working right?

I am trying to open a url in the same page when the user presses a button. But when I do this code on button press:

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


It will open to this url:

MYSERVER/www.google.com


Instead of just "www.google.com" ("MYSERVER" being where the html/js file is on).

How do I get it to be just "www.google.com" instead of "MYSERVER/www.google.com"?

Any help will be appreciated.

Answer

Change to:

 window.open("http://www.google.com", "_self");

With Javascript (unlike typing in the browser bar), you need to use a fully qualified URL or the JS will try to make it relative to the current page.