What is the best (correct, modern, cross-browser, safe) way to get a web browser to navigate to a URL of your choice using JavaScript?
-
4Not a duplicate. The other question is about redirection.ftvs– ftvs2019-07-05 09:40:13 +00:00Commented Jul 5, 2019 at 9:40
-
1Not a duplicate, but related. The other question doesn't have this concise answer that clarified the solution to my problem.Jesse– Jesse2021-01-28 08:29:03 +00:00Commented Jan 28, 2021 at 8:29
-
Ensure you use the "http://" or "https://" prefixJason– Jason2021-04-09 23:42:24 +00:00Commented Apr 9, 2021 at 23:42
Add a comment
|
3 Answers
This works in all browsers:
window.location.href = '...';
If you wanted to change the page without it reflecting in the browser back history, you can do:
window.location.replace('...');
6 Comments
Oliver
Using
window.location = '...' is a synonym of window.location.href = '...' - from Window.location API.masterxilo
If you want to avoid reloading the whole page (though that would not technically be considered a navigation), look into history.pushState and history.replaceState.
Agnel Vishal
If you want to simulate clicking on a link, use
location.href If you want to simulate an HTTP redirect, use location.replace Note that location.replace does not keep the originating page in the session history.Ben Wheeler
you should also mention
window.location.assign('...'), which is functionally equivalent to window.location.href = '...'.HischT
@425nesp for target="_blank" behavior you should use window.open("url", "_blank");
|
Try these:
window.location.href = 'http://www.google.com';window.location.assign("http://www.w3schools.com");window.location = 'http://www.google.com';
For more see this link: other ways to reload the page with JavaScript
4 Comments
Michael Scheper
This answer would be more helpful if it explained the difference between the three. Also, please refer to 'Provide context for links' at stackoverflow.com/help/how-to-answer, because the link doesn't provide any more info, either.
Seth Eden
If you are using TestCafe with Node.js then you could also do:
await t.navigateTo('http://www.google.com');Joe
is there a way to wait for the page to full load after any of these or similar calls? e.g. if I run this in Chrome Console, only last page (google) ever truly loads window.location.href = 'google.com'; window.location.assign("w3schools.com"); window.location = 'google.com';
Javad-M
document.location = "..."; document.location.href ="..." ; document.location.assign("..."); document.location.replace("...."); can also be used.It seems that this is the correct way window.location.assign("http://www.mozilla.org");
3 Comments
Or Betzalel
Good documentation but it says that the assign function doesn't save the current address in the history and thats something to consider. There is no correct or incorrect way, depends on the programmer's needs
Ben Wheeler
@OrBetzalel, I believe the assign function DOES save the current address in the browser's history (though of course that could vary across browsers). See discussion at developer.mozilla.org/en-US/docs/Web/API/Location, where it explains that
window.location.replace() does NOT save the current address in history, but makes no such mention of window.location.assign(). In my own testing with Chrome (current Mac version as of Oct 2019), window.location.assign() DOES save the current address in the browser's history.ahuigo
In my own testing with Chrome (current Mac version as of April 2023), window.location.assign() DOES NOT save the current address in the browser's history.