Javascript window.location.href, window.location.replace 和 window.location.assign 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7703689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:15:18  来源:igfitidea点击:

Difference between window.location.href, window.location.replace and window.location.assign

javascriptdomlocation

提问by milan_9211

What is the difference between

之间有什么区别

  1. window.location.href="http://example.com";
  2. window.location.replace("http://example.com");
  3. window.location.assign("http://example.com");
  1. window.location.href="http://example.com";
  2. window.location.replace("http://example.com");
  3. 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.

我在许多论坛上读到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>

回答by Guffa

These do the same thing:

它们做同样的事情:

window.location.assign(url);
window.location = url;
window.location.href = url;

They simply navigate to the new URL. The replacemethod on the other hand navigates to the URL without adding a new record to the history.

他们只需导航到新的 URL。replace另一方面,该方法导航到 URL,而不向历史记录添加新记录。

So, what you have read in those many forms is not correct. The assignmethod does add a new record to the history.

因此,您以多种形式阅读的内容是不正确的。该assign方法确实向历史记录添加了新记录。

Reference: http://developer.mozilla.org/en/window.location

参考:http: //developer.mozilla.org/en/window.location

回答by Chuck Kollars

The part about not being able to use the Back button is a common misinterpretation. window.location.replace(URL) throws out the topONE 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 entirepage history list, nor does it make the Back button completely non-functional.

关于无法使用“后退”按钮的部分是一种常见的误解。window.location.replace(URL)通过用新条目覆盖它从页面历史记录列表中抛出顶部的ONE 条目,因此用户无法轻松返回该 ONE 特定网页。该功能不会清除整个页面历史记录列表,也不会使后退按钮完全失效。

(NO function nor combination of parameters that I know of can change or overwrite history list entries that youdon'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 topone 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 reallywhat you want to do), "open" a brand new window. (You can "open" a popup that doesn't even havea "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.

如果您真的想让后退按钮不起作用(可能不是“用户友好”:如果您真的想要这样做,请再想一想),请“打开”一个全新的窗口。(您也可以“打开”一个甚至没有“后退”按钮的弹出窗口……但是现在弹出窗口不是很流行:-) 如果您想让您的页面无论用户做什么都显示(再次“用户友好性”是有问题的),设置一个 window.onunload 处理程序,每次都从头开始重新加载您的页面。