window.location.href vs window.location.replace vs window.location.assign in JavaScript

difference, dom, javascript, location, url

Solution

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

Problem

What is the difference between - `window.location.href="http://example.com";` - `window.location.replace("http://example.com");` - `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> ```

Original source

Related problems