Difference between window.location.href=window.location.href and window.location.reload()

Javascript

Javascript Problem Overview


What is the difference between JavaScript's

window.location.href = window.location.href

and

window.location.reload()

functions?

Javascript Solutions


Solution 1 - Javascript

If I remember correctly, window.location.reload() reloads the current page with POST data, while window.location.href=window.location.href does not include the POST data.

As noted by @W3Max in the comments below, window.location.href=window.location.href will not reload the page if there's an anchor (#) in the URL - You must use window.location.reload() in this case.

Also, as noted by @Mic below, window.location.reload() takes an additional argument skipCache so 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.

Solution 2 - Javascript

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.

Note: default value for window.location.reload() is false

Solution 3 - Javascript

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

window.location.reload();

will reload the page.

Solution 4 - Javascript

If you add the boolean true to the reload window.location.reload(true) it will load from server.

It is not clear how supported this boolean is, W3Org mentions that NS used to support it

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.location that had to do with redirection, but that is really last millennium.

For documentation purposes I would use window.location.reload() because that is what you want to do.

Solution 5 - Javascript

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:

if (!window.location.hash) {
    window.location.href = window.location.href;
} else {
    window.location.reload();
}

Solution 6 - Javascript

Came across this question researching some aberrant behavior in IE, specifically IE9, didn't check older versions. It seems

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.URL will return much quicker and with less traffic.

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.

Solution 7 - Javascript

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.

Google Chrome does a GET for both.

Solution 8 - Javascript

Using JSF, I'm now having the issue with refresh after session is expired: https://stackoverflow.com/questions/15252234/primefaces-viewexpiredexception-after-page-reload and with some investigation I have found one difference in FireFox:

Calling window.location.reload() works like clicking refresh icon on FF, it adds the line

Cache-Control max-age=0

while setting window.location.href works like pressing ENTER in URL line, it does not send that line.

Though both are sent as GET, the first (reload) is restoring the previous data and the application is in inconsistent state.

Solution 9 - Javascript

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.

Solution 10 - Javascript

from my experience of about 3 years, i could not find any difference...

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

Solution 11 - Javascript

In our case we just want to reload the page in webview and for some reasons we couldn't find out why! We try almost every solution that has been on the web, but stuck with no reloading using location.reload() or alternative solutions like window.location.reload(), location.reload(true), ...!

Here is our simple solution :

Just use a < a > tag with the empty "href" attribution value like this :

< a href="" ...>Click Me</a>

(in some cases you have to use "return true" on click of the target to trigger reload)

For more information check out this question : https://stackoverflow.com/questions/5637969/is-an-empty-href-valid

Solution 12 - Javascript

window.location.href, this as saved my life in webview from Android 5.1. The page don't reload with location.reload() in this version from Android.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrianView Question on Stackoverflow
Solution 1 - JavascriptDavid JohnstoneView Answer on Stackoverflow
Solution 2 - JavascriptMicView Answer on Stackoverflow
Solution 3 - JavascriptFabien MénagerView Answer on Stackoverflow
Solution 4 - JavascriptmplungjanView Answer on Stackoverflow
Solution 5 - JavascriptminteriorView Answer on Stackoverflow
Solution 6 - JavascriptinvertedSpearView Answer on Stackoverflow
Solution 7 - JavascriptmrjView Answer on Stackoverflow
Solution 8 - JavascriptDanubian SailorView Answer on Stackoverflow
Solution 9 - JavascriptOlliView Answer on Stackoverflow
Solution 10 - JavascriptkumarharshView Answer on Stackoverflow
Solution 11 - JavascriptMetro PolisueView Answer on Stackoverflow
Solution 12 - JavascriptRony SilvaView Answer on Stackoverflow