What’s the difference between window.location= and window.location.replace()?

window.location adds an item to your history in that you can (or should be able to) click “Back” and go back to the current page.

window.location.replace replaces the current history item so you can’t go back to it.

See window.location:

assign(url): Load the document at
the provided URL.

replace(url):Replace the current
document with the one at the provided
URL. The difference from the
assign() method is that after using
replace() the current page will not
be saved in session history, meaning
the user won’t be able to use the Back
button to navigate to it.

Oh and generally speaking:

window.location.href = url;

is favoured over:

window.location = url;

Leave a Comment