How to get the browser to navigate to URL in JavaScript [duplicate] – Dev

The best answers to the question “How to get the browser to navigate to URL in JavaScript [duplicate]” in the category Dev.

QUESTION:

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?

ANSWER:

Try these:

  1. window.location.href="http://www.google.com";
  2. window.location.assign("http://www.w3schools.com");
  3. window.location = 'http://www.google.com';

For more see this link: other ways to reload the page with JavaScript

ANSWER:

This works in all browsers:

window.location.href="https://stackoverflow.com/questions/1226714/...";

If you wanted to change the page without it reflecting in the browser back history, you can do:

window.location.replace("https://stackoverflow.com/questions/1226714/...");

ANSWER:

It seems that this is the correct way window.location.assign("http://www.mozilla.org");

  • https://developer.mozilla.org/en-US/docs/Web/API/Window/location
  • https://www.w3schools.com/js/js_window_location.asp
  • https://jstricks.com/javascript-redirect-page-redirection/