1172

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jjujuma
  • 22,055
  • 12
  • 44
  • 46

3 Answers3

1897

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('...');
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 47
    Using `window.location = '...'` is a synonym of `window.location.href = '...'` - from [Window.location API](https://developer.mozilla.org/en-US/docs/Web/API/Window/location). – Oliver Mar 30 '16 at 09:09
  • 4
    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. – masterxilo May 06 '16 at 00:11
  • 3
    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. – Agnel Vishal Aug 23 '19 at 13:09
  • 3
    you should also mention `window.location.assign('...')`, which is functionally equivalent to `window.location.href = '...'`. – Ben Wheeler Oct 18 '19 at 17:12
144

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

Ethan
  • 4,295
  • 4
  • 25
  • 44
tilak
  • 4,589
  • 6
  • 34
  • 45
  • 107
    This answer would be more helpful if it explained the difference between the three. Also, please refer to 'Provide context for links' at http://stackoverflow.com/help/how-to-answer, because the link doesn't provide any more info, either. – Michael Scheper Oct 01 '15 at 01:14
  • If you are using TestCafe with Node.js then you could also do: `await t.navigateTo('http://www.google.com');` – Seth Eden Nov 08 '17 at 14:56
  • `location.assign` worked in my case, thank you :) – vintprox Oct 03 '19 at 07:02
  • 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 = 'http://www.google.com'; window.location.assign("http://www.w3schools.com"); window.location = 'http://www.google.com'; – Joe Jan 04 '20 at 07:59
  • `document.location = "..."; document.location.href ="..." ; document.location.assign("..."); document.location.replace("....");` can also be used. – Javad-M Jun 19 '23 at 19:43
63

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

drac_o
  • 427
  • 5
  • 11
Bogdan
  • 1,323
  • 15
  • 15
  • 28
    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 – Or Betzalel Jun 01 '15 at 09:45
  • 4
    @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 https://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. – Ben Wheeler Oct 18 '19 at 17:11
  • 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. – ahuigo Apr 04 '23 at 14:05