Difference between window.location.href=window.location.href and window.location.reload()
Asked Answered
E

12

226

What is the difference between JavaScript's

window.location.href = window.location.href

and

window.location.reload()

functions?

Elevate answered 8/3, 2010 at 22:0 Comment(0)
A
283

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.

Affluent answered 8/3, 2010 at 22:14 Comment(7)
note that when you use window.location.reload() on a POST the browser will ask you if you want resend the data to reload the pageStamm
@Wimmel, is there a way to disable this message ?Autodidact
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.Encyst
Also note that location.reload() will also force reload all static content (much like a ctrl+f5 style hard refresh) whereas setting location.href back to href (or pathname or URL) does not, which could be a significant (and unnecessary) difference in load time on some pages.Athanasius
@Wimmel Chrome: reloads the page with a GET Firefox: re-executes the previous request, meaning if it was a POST, you'll get the nice popup asking you about whether to resend the data or notPlatinumblond
If you need to modify the url without reloading and without a '#' you could use window.history.pushState(state, title, URL);Hellfire
You can use location.href=location.search and it will work even with # in URL.Fico
N
56

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

Nicolas answered 8/3, 2010 at 22:10 Comment(4)
@Ismail - The default is false.Alysa
Google Chrome 32, while using webRTC the true/false did not worked for me. I had a iframe with webRTC and only using window.location.href = window.location.href did the trick.Thormora
If you have made changes to a form on the page, the changes may disappear (revert to cached values) depending on the browser when using location.reload() or location.reload(false). To do a complete refresh of the page, use location.reload(true).Rhomb
Note that the forceGet parameter for location.reload() is only available in Firefox!Siu
U
33

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.

Ugric answered 23/2, 2011 at 13:36 Comment(3)
Not all browsers have this issue with ending hases. If ending hashes are a concern for you, try: window.location = document.URL.replace(/#$/, '');Phenyl
At least Chrome is concerned. I used to take location.href = location.href for granted, but I just noticed that exact behavior and came to SO to spread the word. Just use location.reload() instead.Plump
You can also use window.location.pathname instead of writing such regular expression. For example: window.location.replace(window.location.pathname);Durkin
B
20

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.

Bair answered 23/2, 2011 at 13:24 Comment(1)
Look at https://mcmap.net/q/41229/-difference-between-window-location-href-window-location-href-and-window-location-reload since this explains the difference.Interior
H
16

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();
}
Hoang answered 13/6, 2013 at 8:48 Comment(0)
B
7

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.

Byron answered 19/9, 2011 at 22:12 Comment(1)
window.location = document.URL reloads the page just like window.location.reload(). Is there a state of the art for refreshing without scrolling back to the top, or imperceptibly as you said?Grugru
P
6

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.

Pony answered 16/5, 2012 at 6:49 Comment(1)
Chrome 38 now seems to use POST for .reload().Ridley
L
3

Using JSF, I'm now having the issue with refresh after session is expired: 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.

Lazarus answered 7/3, 2013 at 8:35 Comment(0)
N
1

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.

Nematic answered 23/2, 2011 at 13:23 Comment(0)
E
1

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

Engels answered 23/2, 2011 at 13:24 Comment(0)
C
0

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 : Is an empty href valid?

Ciliate answered 9/10, 2019 at 11:18 Comment(0)
R
-4

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.

Regenerator answered 27/4, 2018 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.