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.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。另一方面,替换
方法导航到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.
所以,你在这些形式中阅读的内容是不正确的。 分配方法会在历史记录中添加新记录。
So, what you have read in those many forms is not correct. The assign
method does add a new record to the history.
参考: http://developer.mozilla.org/en/window.location
这篇关于window.location.href,window.location.replace和window.location.assign之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!