Reload a page with location.href or window.location.reload(true)

风流意气都作罢 提交于 2020-07-17 06:28:25

问题


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?


回答1:


The main difference is follow:

window.location.reload() reloads the current page with POST data, while window.location.href='your url' does not include the POST 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.



来源:https://stackoverflow.com/questions/41020403/reload-a-page-with-location-href-or-window-location-reloadtrue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!