简体   繁体   English

window.location.assign() 和 window.location.replace() 的区别

[英]Difference between window.location.assign() and window.location.replace()

What is the difference between window.location.assign() and window.location.replace() , when both redirect to a new page? window.location.assign()window.location.replace()都重定向到新页面时有什么区别?

Using window.location.assign("url") will just cause a new document to load.使用window.location.assign("url")只会导致加载新文档。 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.使用window.location.replace("url")将替换当前文档并用该 URL 替换当前历史记录,这样您就无法返回到上一个加载的文档。

Reference: http://www.exforsys.com/tutorials/javascript/javascript-location-object.html参考: 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: 根据 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.assign()方法的不同之处在于,在使用replace()之后,当前页面将不会保存在会话历史记录中,这意味着用户将无法使用“后退”按钮导航到它。

  1. location.assign():位置。分配():

    To assign route path by passing path into it.通过将路径传递给它来分配路由路径。 Assign will give you a history even after path was assigned.即使在分配路径后,Assign 也会为您提供历史记录。

    Usage Method: Value should be passed into it.使用方法:要传入值。

    Eg: location.assign("http://google.com")例如: location.assign("http://google.com")

位置.分配()

  1. 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")例如: location.replace("http://google.com")

location.repalce()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM