The Location. assign() method causes the window to load and display the document at the URL specified. After the navigation occurs, the user can navigate back to the page that called Location.
window. location is read/write on all compliant browsers. document. location is read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).
Using window.location.assign("url")
will just cause a new document to load. Using window.location.replace("url")
will replace the current document and replace the current History with that URL making it so you can't go back to the previous document loaded.
Reference: http://www.exforsys.com/tutorials/javascript/javascript-location-object.html
The difference is how history is handled. "Replace" won't give you history, "assign" will.
According to MDN:
The difference from the
assign()
method is that after usingreplace()
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.
location.assign():
To assign route path by passing path into it. Assign will give you a history even after path was assigned.
Usage Method: Value should be passed into it.
Eg: location.assign("http://google.com")
location.replace():
It helps to replace path if you don't want to keep history. It won't give you a history once you replace its path.
Usage Method: Value should be passed into it.
Eg: location.replace("http://google.com")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With