Reload the page using location.href or window.location.reload (true)

I need to reload the page with ajax call success.

I see the 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 a difference in behavior? I know the difference in both location and window.location, but in terms of job execution?

+6
source share
1 answer

The main difference is as follows:

window.location.reload () reloads the current page with POST data, and window.location.href = 'your url' does not include POST data.

In addition, window.location.reload(true) reloads the page from the server. And the browser will skip the cache.

For example, I see that you are using the success function from an AJAX request.

Suppose you follow the method:

 [OutputCache(Duration=600)] public ActionResult Homepage(){ //code here return View(); } 

If you use window.location.href="location_URL" , then the browser cache data is 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 reload the page from the server.

+8
source

Source: https://habr.com/ru/post/1012976/


All Articles