Javascript window.location.href=window.location.href 和 window.location.reload() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2405117/
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
Difference between window.location.href=window.location.href and window.location.reload()
提问by Brian
What is the difference between JavaScript's
JavaScript 的有什么区别
window.location.href = window.location.href
and
和
window.location.reload()
functions?
职能?
回答by David Johnstone
If I remember correctly, window.location.reload()reloads the current page with POST data, while window.location.href=window.location.hrefdoes not include the POST data.
如果我没记错的话,window.location.reload()用 POST 数据重新加载当前页面,window.location.href=window.location.href而不包括 POST 数据。
As noted by @W3Max in the comments below, window.location.href=window.location.hrefwill not reload the page if there's an anchor (#) in the URL - You must use window.location.reload()in this case.
正如@W3Max 在下面的评论中所指出的,window.location.href=window.location.href如果 URL 中有锚点 (#) ,则不会重新加载页面 -window.location.reload()在这种情况下,您必须使用。
Also, as noted by @Mic below, window.location.reload()takes an additional argument skipCacheso that with using window.location.reload(true)the browser will skip the cache and reload the page from the server. window.location.reload(false)will do the opposite, and load the page from cache if possible.
此外,正如下面@Mic 所指出的,window.location.reload()需要一个额外的参数,skipCache以便使用window.location.reload(true)浏览器将跳过缓存并从服务器重新加载页面。window.location.reload(false)将做相反的事情,并在可能的情况下从缓存加载页面。
回答by Mic
If you say window.location.reload(true)the browser will skip the cache and reload the page from the server. window.location.reload(false)will do the opposite.
如果您说window.location.reload(true)浏览器将跳过缓存并从服务器重新加载页面。window.location.reload(false)会做相反的事情。
Note: defaultvalue for window.location.reload()is false
注:default为值window.location.reload()ISfalse
回答by Fabien Ménager
The difference is that
不同之处在于
window.location = document.URL;
will not reload the page if there is a hash (#) in the URL (with or without something after it), whereas
如果 URL 中有哈希 (#)(后面有或没有),则不会重新加载页面,而
window.location.reload();
will reload the page.
将重新加载页面。
回答by mplungjan
If you add the boolean true to the reload
window.location.reload(true)it will load from server.
如果您将布尔值 true 添加到重新加载
window.location.reload(true),它将从服务器加载。
It is not clear how supported this boolean is, W3Orgmentions that NS used to supportit
不清楚这个布尔值的支持程度,W3Org提到 NS曾经支持它
There MIGHT be a difference between the content of window.location.href and document.URL- there at least used to be a difference between location.href and the non-standard and deprecated document.locationthat had to do with redirection, but that is really last millennium.
window.location.href 和document.URL的内容之间可能存在差异- 至少在 location.href 和与重定向有关的非标准和不推荐使用的document.location之间曾经存在差异,但是真的是上个千年。
For documentation purposes I would use window.location.reload()because that is what you want to do.
出于文档目的,我将使用window.location.reload()因为这是您想要做的。
回答by minterior
As said, modifying the href when there is a hash (#) in the url would not reload the page. Thus, I use this to reload it instead of regular expressions:
如上所述,当 url 中有哈希 (#) 时修改 href 不会重新加载页面。因此,我使用它来重新加载它而不是正则表达式:
if (!window.location.hash) {
window.location.href = window.location.href;
} else {
window.location.reload();
}
回答by invertedSpear
Came across this question researching some aberrant behavior in IE, specifically IE9, didn't check older versions. It seems
在研究 IE 中的一些异常行为时遇到了这个问题,特别是 IE9,没有检查旧版本。它似乎
window.location.reload();
results in a refresh that blanks out the entire screen for a second, where as
导致刷新使整个屏幕空白一秒钟,其中
window.location = document.URL;
refreshes the page much more quickly, almost imperceptibly.
几乎在不知不觉中更快地刷新页面。
Doing a bit more research, and some experimentation with fiddler, it seems that window.location.reload()will bypass the cache and reload from the server regardless if you pass the boolean with it or not, this includes getting all of your assets (images, scripts, style sheets, etc) again. So if you just want the page to refresh the HTML, the window.location = document.URLwill return much quicker and with less traffic.
做更多的研究,并使用 fiddler 进行一些实验,似乎window.location.reload()会绕过缓存并从服务器重新加载,无论您是否通过它传递布尔值,这包括获取所有资产(图像、脚本、样式表、等)再次。因此,如果您只希望页面刷新 HTML,window.location = document.URL它将以更少的流量返回得更快。
A difference in behavior between browsers is that when IE9 uses the reload method it clears the visible page and seemingly rebuilds it from scratch, where FF and chrome wait till they get the new assets and rebuild them if they are different.
浏览器之间的行为差异在于,当 IE9 使用 reload 方法时,它会清除可见页面并从头开始重建它,其中 FF 和 chrome 等待它们获取新资产,如果它们不同,则重建它们。
回答by mrj
A difference in Firefox (12.0) is that on a page rendered from a POST, reload() will pop up a warning and do a re-post, while a URL assignment will do a GET.
Firefox (12.0) 的不同之处在于,在从 POST 呈现的页面上,reload() 将弹出警告并重新发布,而 URL 分配将执行 GET。
Google Chrome does a GET for both.
谷歌浏览器对两者都做了 GET。
回答by Danubian Sailor
Using JSF, I'm now having the issue with refresh after session is expired: PrimeFaces ViewExpiredException after page reloadand with some investigation I have found one difference in FireFox:
使用 JSF,我现在遇到了会话过期后刷新的问题:页面重新加载后的 PrimeFaces ViewExpiredException并通过一些调查我发现了 FireFox 的一个区别:
Calling window.location.reload()works like clicking refresh icon on FF, it adds the line
调用window.location.reload()就像点击 FF 上的刷新图标一样,它添加了一行
Cache-Control max-age=0
Cache-Control max-age=0
while setting window.location.hrefworks like pressing ENTER in URL line, it does not send that line.
虽然设置window.location.href就像在 URL 行中按 ENTER 一样,但它不会发送该行。
Though both are sent as GET, the first (reload) is restoring the previous data and the application is in inconsistent state.
虽然两者都是作为 GET 发送的,但第一个(重新加载)正在恢复以前的数据并且应用程序处于不一致的状态。
回答by Olli
No, there shouldn't be. However, it's possible there is differences in some browsers, so either (or neither) may not work in some case.
不,不应该有。但是,某些浏览器可能存在差异,因此在某些情况下(或两者都不)可能无法正常工作。
回答by kumarharsh
from my experience of about 3 years, i could not find any difference...
从我大约 3 年的经验来看,我找不到任何区别......
edit : yes, as one of them here has said, only passing a boolean parameter to window.location.reload() is the difference. if you pass true, then the browser loads a fresh page, but if false, then the cache version is loaded...
编辑:是的,正如其中一位所说,只有将布尔参数传递给 window.location.reload() 才是区别。如果传递true,则浏览器加载一个新页面,但如果传递false,则加载缓存版本...

