使用 location.href 或 window.location.reload(true) 重新加载页面 [英] Reload a page with location.href or window.location.reload(true)

查看:190
本文介绍了使用 location.href 或 window.location.reload(true) 重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }

success : function(obj) {
//code
        window.location.reload(true);
    }

行为上有什么不同吗?我知道 location 和 window.location 的区别,但就工作而言?

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.reload() 使用 POST 数据重新加载当前页面,而 window.location.href='your url' 不包含 POST 数据.

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

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

Further more, window.location.reload(true) method reload page from the server. And the browser will skip the cache.

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

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

假设你有以下方法:

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

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

If you are using window.location.href="location_URL",then browser cache data for 600 seconds, which means 10 minutes.

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

On the other hand, if you use window.location.reload(true), then the browser will skip the cache and ,then, reload page from server.

这篇关于使用 location.href 或 window.location.reload(true) 重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆