javascript - window.location= 和 window.location.replace() 有什么区别?

标签 javascript window location

这两行有区别吗?

var url = "http://www.google.com/";
window.location = url;
window.location.replace(url);

最佳答案

window.location 将一个项目添加到您的历史记录中,您可以(或应该能够)单击“返回”并返回当前页面。

window.location.replace 替换当前历史记录项,因此您无法返回。

window.location :

assign(url): Load the document at the provided URL.

replace(url):Replace the current document 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.

哦,一般来说:

window.location.href = url;

被青睐:

window.location = url;

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

相关文章:

javascript - typeahead - minLength 触发建议列表

javascript - 使用 Javascript 或 Jquery 将整数转换为精度为 2 的 float

youtube - 如何限制Youtube Player全屏仍在窗口内,我不想拥有真正的全屏

javascript - 我可以以某种方式使用目标 =""——还是必须使用 javascript 来满足这个要求?

android - ICS Android 以编程方式启用 gps?

javascript - 如何混合像这样的 anchor 和背景图片?

javascript - 将所有 jquery 脚本转换为实时脚本

Angular 4 Portal/CdkPortal 在新的子窗口中不工作

java - 定位器响应

javascript - 涉及日期 fns 格式的时区问题()