Difference between window.location.assign() and window.location.replace()

二次信任 提交于 2019-11-26 10:25:00

问题


What is the difference between window.location.assign() and window.location.replace(), when both redirect to a new page?


回答1:


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




回答2:


The difference is how history is handled. "Replace" won't give you history, "assign" will.




回答3:


According to MDN:

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.




回答4:


  1. 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")

  1. 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")



来源:https://stackoverflow.com/questions/4505798/difference-between-window-location-assign-and-window-location-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!