window.location.assign() 和 window.location.replace() 之间的区别
当两者都重定向到新页面时,window.location.assign()
和 window.location.replace()
之间有什么区别?
What is the difference between window.location.assign()
and window.location.replace()
, when both redirect to a new page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
window.location.assign("url")< /code>
只会导致加载新文档。使用
window.location.replace("url")< /code>
将替换当前文档,并将当前历史记录替换为该 URL,这样您就无法返回到上一个加载的文档。
参考:http://www.exforsys.com/tutorials/javascript/javascript -location-object.html
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
区别在于如何处理历史。 “替换”不会给你历史记录,“分配”会。
The difference is how history is handled. "Replace" won't give you history, "assign" will.
根据 MDN:
According to MDN:
location.assign():
通过传递路径来分配路由路径。即使在分配路径之后,分配也会为您提供历史记录。
使用方法:传入值。
例如:
location.assign("http://google.com")
location.replace():
如果您不想保留历史记录,它有助于替换路径。一旦您替换了路径,它就不会为您提供历史记录。
使用方法:传入值。
例如:
location.replace("http://google.com")
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")