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

发布于 2024-10-08 10:10:01 字数 107 浏览 8 评论 0原文

当两者都重定向到新页面时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

也只是曾经 2024-10-15 10:10:01

使用 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. 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

夏天碎花小短裙 2024-10-15 10:10:01

区别在于如何处理历史。 “替换”不会给你历史记录,“分配”会。

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

伊面 2024-10-15 10:10:01

根据 MDN:

assign()方法的区别在于,使用replace()后,当前页面不会保存在会话历史记录中,这意味着用户不会能够使用“后退”按钮导航到它。

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.

孤者何惧 2024-10-15 10:10:01
  1. location.assign():

    通过传递路径来分配路由路径。即使在分配路径之后,分配也会为您提供历史记录。

    使用方法:传入值。

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

location.assign()

  1. location.replace():

    如果您不想保留历史记录,它有助于替换路径。一旦您替换了路径,它就不会为您提供历史记录。

    使用方法:传入值。

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

location.repalce()

  1. 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.assign()

  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.repalce()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文