Difference between window.location.href=window.location.href and window.location.reload()
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.
Reload a page with location.href or window.location.reload(true)
The main difference is follow:
window.location.reload() reloads the current page with
POST
data, while window.location.href='your url' does not include thePOST
data.
Further more, window.location.reload(true)
method reload page from the server. And the browser will skip the cache.
For example, I see you are using success
function from an AJAX
request.
Suppose you have follow method:
[OutputCache(Duration=600)]
public ActionResult Homepage(){
//code here
return View();
}
If you are using window.location.href="location_URL"
,then browser cache data for 600
seconds, which means 10 minutes.
On the other hand, if you use window.location.reload(true)
, then the browser will skip the cache and ,then, reload page from server.
window.location.href is not Refreshing/Reloading the page
To relaod a page simply use
window.location.reload()
Is there any difference between window.location and window.location.href?
Yes, there is a difference. window.location
is a Location
object. window.location.href
is a string representation of the location. The location
object's toString()
value is the same as the href
property, so they are identical if used as strings. Setting window.location
is the same as setting window.location.href
.
window.location
, however, has several other properties you can use, such as location.hostname
, location.pathname
and location.hash
. So you could could set location.hash
on its own to alter the hash value.
Why location.reload() is slower than the other page reload methods?
Been looking for this myself and the best reference I could find is actually on w3schools.com
http://www.w3schools.com/jsref/met_loc_reload.asp
location.reload(forceGet)
forceGet:
false - Default. Reloads the current page from the cache.
true - The current page must be reloaded from the server
Difference between window.location.href and top.location.href
window.location.href
returns the location of the current page.
top.location.href
(which is an alias of window.top.location.href
) returns the location of the topmost window in the window hierarchy. If a window has no parent, top
is a reference to itself (in other words, window
=== window.top
).
top
is useful both when you're dealing with frames and when dealing with windows which have been opened by other pages. For example, if you have a page called test.html
with the following script:
var newWin=window.open('about:blank','test','width=100,height=100');
newWin.document.write('<script>alert(top.location.href);</script>');
The resulting alert will have the full path to test.html – not about:blank, which is what window.location.href
would return.
To answer your question about redirecting, go with window.location.assign(url);
Related Topics
Angular 2: How to Detect Changes in an Array? (@Input Property)
Obtain Form Input Fields Using Jquery
How to Get the Anchor from the Url Using Jquery
How to Copy Text from a Div to Clipboard
How to Define Custom Sort Function in JavaScript
What Is "Function*" in JavaScript
Encrypt in PHP Openssl and Decrypt in JavaScript Cryptojs
Create a String of Variable Length, Filled with a Repeated Character
How to Swipe Top Down Jquery Mobile
"Pointer-Events: None" Does Not Work in IE9 and Ie10
How to Conditionally Add Attributes to React Components
How to Post with Multipart Form Data Using Fetch
Is There a Good JavaScript Minifier
How to Remove One Specific Selected File from Input File Control
Regex to Check Whether a String Contains Only Numbers
Get the Content (Text) of an Url After JavaScript Has Run with PHP