简体   繁体   中英

window.location.href = window.location.href and JSLint

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?

EDIT window.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.

UPDATE I 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 as window.location.href = window.location.href in all these browsers.

Here's the test script .

Try this

window.location = window.location.href;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM