181

What is the difference between

  1. window.location.href="http://example.com";
  2. window.location.replace("http://example.com");
  3. window.location.assign("http://example.com");

I read in many forums that window.location.assign() just replaces the current session history and hence back button of browser will not function. However, I am not able to reproduce this.

function fnSetVariable() {
    //window.location.href = "http://example.com";
    window.location.replace("http://example.com");
    //window.location.assign("http://example.com");
}

<a onmouseover="fnSetVariable();" 
   href="PageCachingByParam.aspx?id=12" >
   CLICK 
</a>
3

3 Answers 3

227

These do the same thing:

window.location.assign(url);
window.location = url;
window.location.href = url;

They simply navigate to the new URL. The replace method on the other hand navigates to the URL without adding a new record to the history.

So, what you have read in those many forums is not correct. The assign method does add a new record to the history.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/location

5
  • 1
    Thanks a lot for the answer. It helped me a lot to resolve the browser back button issue. Commented Apr 12, 2013 at 7:29
  • @blunderboy: It's the top answer by a landslide anyway, so that wouldn't change a thing.
    – BoltClock
    Commented Sep 27, 2014 at 18:11
  • So what's the point of assign()? From this answer, and the docs, it sounds identical to location = ....
    – Mitya
    Commented May 28, 2017 at 18:26
  • 3
    @Mitya IMHO assign() is better, calling a function for a side effect is more readable. Also you can easily mock it for testing. Commented Sep 14, 2020 at 15:08
  • I would like to add that Quttera Labs has marked window.location.replace as potentially suspicious.
    – Divern
    Commented Apr 29, 2021 at 4:20
18

The part about not being able to use the Back button is a common misinterpretation. window.location.replace(URL) throws out the top ONE entry from the page history list, by overwriting it with the new entry, so the user can't easily go Back to that ONE particular webpage. The function does NOT wipe out the entire page history list, nor does it make the Back button completely non-functional.

(NO function nor combination of parameters that I know of can change or overwrite history list entries that you don't own absolutely for certain - browsers generally impelement this security limitation by simply not even defining any operation that might at all affect any entry other than the top one in the page history list. I shudder to think what sorts of dastardly things malware might do if such a function existed.)

If you really want to make the Back button non-functional (probably not "user friendly": think again if that's really what you want to do), "open" a brand new window. (You can "open" a popup that doesn't even have a "Back" button too ...but popups aren't very popular these days:-) If you want to keep your page showing no matter what the user does (again the "user friendliness" is questionable), set up a window.onunload handler that just reloads your page all over again clear from the very beginning every time.

4
  • 4
    'NO function nor combination of parameters that I know of can change or overwrite history list entries ' ... Welcome to HTML5
    – SpYk3HH
    Commented Jan 29, 2014 at 13:59
  • 7
    The inability to change or overwrite browser history entries you don't own is a security rule that's been around for a long time. HTML5 simply continues that rule. Commented Feb 27, 2014 at 17:31
  • You missed the point or sarcasm rather. Look up html5 and history
    – SpYk3HH
    Commented Feb 27, 2014 at 19:19
  • window.location.replace is not working for the local file path (eg: file:///C:/1.html)
    – Loch
    Commented Mar 17, 2022 at 10:25
0

location, location.href and location.assign() can go to the URL adding the record to the history so we can go back to the previous page:

location = "http://example.com";
location.href = "http://example.com";
location.assign("http://example.com");

location.replace() can go to the URL not adding the record to the history so we cannot go back to the previous page:

location.replace("http://example.com");

Not the answer you're looking for? Browse other questions tagged or ask your own question.