What is the difference between window.location.assign()
and window.location.replace()
, when both redirect to a new page?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
According to MDN:
Using
window.location.assign("url")
will just cause a new document to load. Usingwindow.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
location.assign():
to assign route path by passing path into it.. Assign will give you an history even after path was assigned.
Usage Method: value should be pass into it.
Eg: location.assign("http://google.com")
location.replace():
it will helps to replace path if you dont want to keep history. it wont give you an history once you replace its path.
Usage Method: value should be pass into it.
Eg: location.replace("http://google.com")
The difference is how history is handled. "Replace" won't give you history, "assign" will.