JavaScript 中的 window.location.href 与 window.location.replace 与 window.location.assign
之间有什么区别
window.location.href="http://example.com";
window.location.replace("http://example.com");
code>window.location.assign("http://example.com");
我在许多论坛上读到 window.location.assign()
只是替换了当前的会话历史记录和浏览器的后退按钮将不起作用。但是,我无法重现这一点。
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>
What is the difference between
window.location.href="http://example.com";
window.location.replace("http://example.com");
window.location.assign("http://example.com");
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们做同样的事情:
它们只是导航到新的 URL。另一方面,
replace
方法导航到 URL,而不向历史记录添加新记录。所以,你在这么多论坛上读到的内容都是不正确的。
assign
方法确实向历史记录中添加了一条新记录。参考:https://developer.mozilla.org/en-US /docs/Web/API/Window/location
These do the same thing:
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 forums is not correct. The
assign
method does add a new record to the history.Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/location
关于无法使用“后退”按钮的部分是一种常见的误解。 window.location.replace(URL) 通过用新条目覆盖它,从页面历史记录列表中抛出top 一个条目,这样用户就无法轻松返回到该特定网页。该功能不会清除整个页面历史列表,也不会使“后退”按钮完全不起作用。
(据我所知,没有任何函数或参数组合可以更改或覆盖您绝对不拥有的历史列表条目 - 浏览器通常通过简单地不定义任何可能的操作来实现此安全限制根本不会影响页面历史记录列表中除顶部之外的任何条目,如果存在这样的功能,我不禁想到恶意软件可能会做出什么样的卑鄙事情。
)按钮不起作用(可能不是“用户友好”:再想一下这是否真的是你想做的),“打开”一个全新的窗口。 (您可以“打开”一个甚至没有“后退”按钮的弹出窗口...但是弹出窗口现在不太流行:-)如果您想保持页面显示无论用户做什么(再次“用户友好性”是有问题的),设置一个 window.onunload 处理程序,每次都从头开始重新加载您的页面。
The part about not being able to use the Back button is a common misinterpretation. window.location.replace(URL) throws out the top ONE entry from the page history list, by overwriting it with the new entry, so the user can't easily go Back to that ONE particular webpage. The function does NOT wipe out the entire page history list, nor does it make the Back button completely non-functional.
(NO function nor combination of parameters that I know of can change or overwrite history list entries that you don't own absolutely for certain - browsers generally impelement this security limitation by simply not even defining any operation that might at all affect any entry other than the top one in the page history list. I shudder to think what sorts of dastardly things malware might do if such a function existed.)
If you really want to make the Back button non-functional (probably not "user friendly": think again if that's really what you want to do), "open" a brand new window. (You can "open" a popup that doesn't even have a "Back" button too ...but popups aren't very popular these days:-) If you want to keep your page showing no matter what the user does (again the "user friendliness" is questionable), set up a window.onunload handler that just reloads your page all over again clear from the very beginning every time.
位置,location.href 和 location.assign() 可以转到添加以下内容的 URL记录到历史记录,以便我们可以返回上一页:
location.replace() 可以转到不将记录添加到历史记录中的 URL,因此我们无法返回到上一页:
location, location.href and location.assign() can go to the URL adding the record to the history so we can go back to the previous page:
location.replace() can go to the URL not adding the record to the history so we cannot go back to the previous page: