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:
.reload()
.reload()
, .reload(true)
, and .reload(false)
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.