window.location.href, window.location.replace 和 window.location.assign 的区别 [英] Difference between window.location.href, window.location.replace and window.location.assign
问题描述
和
window.location.href="http://example.com";
window.location.replace("http://example.com");
window.location.assign("http://example.com");
我在很多论坛上读到 window.location.assign()
只是替换当前会话历史记录,因此浏览器的后退按钮将不起作用.但是,我无法重现这一点.
I read in many forums that window.location.assign()
just replaces the current session history and hence back button of browser will not function. However, I am not able to reproduce this.
function fnSetVariable() {
//window.location.href = "http://example.com";
window.location.replace("http://example.com");
//window.location.assign("http://example.com");
}
<a onmouseover="fnSetVariable();"
href="PageCachingByParam.aspx?id=12" >
CLICK
</a>
推荐答案
这些都做同样的事情:
window.location.assign(url);
window.location = url;
window.location.href = url;
他们只需导航到新 URL.另一方面,replace
方法导航到 URL,而不向历史记录添加新记录.
They simply navigate to the new URL. The replace
method on the other hand navigates to the URL without adding a new record to the history.
所以,你在很多论坛上读到的都是不正确的.assign
方法确实向历史记录添加了一条新记录.
So, what you have read in those many forums is not correct. The assign
method does add a new record to the history.
参考:https://developer.mozilla.org/en-US/docs/Web/API/Window/location
这篇关于window.location.href, window.location.replace 和 window.location.assign 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!