使用location.href或window.location.reload重新加载页面(true)

pmi*_*nda 10 javascript reload window.location location-href

我需要在ajax调用成功时重新加载页面.

我看到了一些代码(不是我的代码),有两种方法:

success : function(obj) {
//code
        location.href = location.href;
    }
Run Code Online (Sandbox Code Playgroud)

要么

success : function(obj) {
//code
        window.location.reload(true);
    }
Run Code Online (Sandbox Code Playgroud)

这种行为有什么不同吗?我知道位置和window.location的区别,但在工作方面?

Mih*_*nut 13

主要区别如下:

window.location.reload()使用POST数据重新加载当前页面,而window.location.href ='your url'不包含POST数据.

此外,window.location.reload(true)方法从服务器重新加载页面.浏览器将跳过缓存.

例如,我看到您正在使用请求中的success函数AJAX.

假设您有以下方法:

[OutputCache(Duration=600)]
public ActionResult Homepage(){
   //code here
   return View();
}
Run Code Online (Sandbox Code Playgroud)

如果您正在使用window.location.href="location_URL",则浏览器缓存数据600数秒,即10分钟.

另一方面,如果您使用window.location.reload(true),则浏览器将跳过缓存,然后从服务器重新加载页面.

  • 此外,如果 URL 中有锚点 (#),`window.location.href=window.location.href` 将不会重新加载页面 (3认同)