javascript - location.replace 和 location.href 有什么区别?

标签 javascript dom

两者有什么区别:

location.replace(url)

location.href = url

我读到第一种方法刺激 HTTP 重定向,第二种方法类似,通过单击链接关注网页。

但是我真的不明白,那是什么意思。

最佳答案

location.replace 不会更新浏览器的历史记录,您不能按后退按钮,location.href 非常类似于单击链接。

The Location.replace()method replaces the current resource with the one at the provided URL. 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.

来源:MDN

关于javascript - location.replace 和 location.href 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19130844/

相关文章:

javascript - 从二维数组中获取列

javascript - 引用错误: document is not defined Jest React Testing Library

javascript - jquery忽略子元素

javascript - 如何关闭由 document.createElement 创建的元素?

html - 是否可以在不重置 DOM 的情况下移动 <video> 元素?

javascript - Jquery,将类添加到创建的元素

javascript - 获得 Node.js 应用程序总执行时间的更好方法是什么?

javascript - 如何在此特定网站上以编程方式模拟用户点击?

javascript - 在 SVG 中使用变换模拟枢轴旋转

javascript - 如何使用 jQuery 的 : . live() 绕过事件气泡