I need to reload a page in a success of an ajax call.
I'm seeing some code (not mine) and there are two ways:
success: function(obj) {
//code
location.href = location.href;
}
or
success: function(obj) {
//code
window.location.reload(true);
}
Is there any difference in the behaviour? I know the difference of both location
and window.location
but in terms of do the job?
window. location. href method returns/loads the current url. So we can use it to reload/refresh the page in javascript.
True reloads the page from the server (e.g. does not store the data cached by the browser): window. location. reload(true);
You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With