javascript window.location.href = window.location.href 和 JSLint
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6751641/
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
window.location.href = window.location.href and JSLint
提问by
If using
如果使用
window.location.href = window.location.href;
to reload a page (without re-POSTing) is not bad practice, what should we make of JSLint's "Weird assignment" complaint?
重新加载页面(不重新发布)是一种不错的做法,我们应该如何看待 JSLint 的“奇怪的分配”投诉?
EDITwindow.location.reload()
is not suitable when you don't want to POST the form data again. In some browsers it provokes the "Resend form data?" which is best avoided when not needed.
window.location.reload()
当您不想再次发布表单数据时,EDIT不适合。在某些浏览器中,它会引发“重新发送表单数据?” 在不需要时最好避免。
UPDATEI did some very brief testing and found:
更新我做了一些非常简短的测试,发现:
- Chrome 12 and Safari 5.0.5 on Mac do not re-POST with
.reload()
- FF 2.0, 3.6, 4.0, 5.0 on Mac present the user with the "resend form dialog" with
.reload()
,.reload(true)
, and.reload(false)
- IE6, IE8(standards), IE8(IE7 mode,standards) in XP; and IE9 and IE10-tech-preview in Win7 behave the same as FF on Mac
window.location = window.location.href
works the same aswindow.location.href = window.location.href
in all these browsers.
- Mac 上的 Chrome 12 和 Safari 5.0.5 不重新发布
.reload()
- FF 2.0,3.6,4.0,5.0在Mac向用户呈现与所述“重发形式对话框”与
.reload()
,.reload(true)
,和.reload(false)
- XP下IE6、IE8(标准)、IE8(IE7模式、标准);Win7 中的 IE9 和 IE10-tech-preview 的行为与 Mac 上的 FF 相同
window.location = window.location.href
与window.location.href = window.location.href
所有这些浏览器的工作方式相同。
Here's the test script.
这是测试脚本。
采纳答案by ShankarSangoli
Try this
试试这个
window.location = window.location.href;
回答by citizen conn
It is indeed a weird assignment to assign something to itself. Sounds like more of a warning than a complaint.
给自己分配一些东西确实是一个奇怪的任务。听起来更像是警告而不是抱怨。
I myself would prefer to use:
我自己更喜欢使用:
window.location.reload()
Edit:but that would repost the form now wouldn't it. Here's a post on SO about the same thing: php reload page without posting data
编辑:但是现在会重新发布表格,不是吗。这是一篇关于 SO 的帖子,关于同样的事情:php reload page without posting data
回答by Nathan Whitehead
It is a weird assignment, JSLint is right. The issue is that setting the variable has the side effect of reloading the page. I would code this as window.location.href = window.location.href + '';
to avoid JSLint errors and put a comment explaining what it does.
这是一个奇怪的任务,JSLint 是对的。问题是设置变量会产生重新加载页面的副作用。我会对此window.location.href = window.location.href + '';
进行编码以避免 JSLint 错误并添加注释来解释它的作用。