Nie*_*sol 5 javascript caching reload
如果您对此问题有更好的标题,请随时编辑.
在最长的时间里,我总是习惯location.reload()重新加载页面 - 这是最合乎逻辑的事情,对吧?
但我最近注意到它并不等同于我最初想过的F5,而是更多的Ctrl + F5.当我想要做的只是重新加载页面时,所有图像和其他链接文件都是从服务器重新请求的.
我发现我可以使用location.replace(location.href),这似乎达到了我想要的效果:重新加载页面,但从缓存中检索链接的文件.
这是理想的吗?有比这更好的方法吗?我忽略了这种方法可能存在的任何陷阱吗?
(注意:我已经对链接文件(如脚本)进行了缓存管理,方法是将其filemtime作为查询字符串附加)
在回答我自己的问题时,存在一个巨大的陷阱:当位置包含哈希时,浏览器将跳转到该哈希而不是重新加载页面。
我实施的解决方案如下:
reload = (function() {
var m = location.search.match(/[?&]__hash=([^&]+)/);
if( m) location.hash = unescape(m[1]);
return function() {
var h = location.hash;
if( h == "") {
location.replace(location.href);
}
else {
var s = location.search;
s = s.replace(/[?&]__hash=[^&]+/,'');
s += (s == "" ? "?" : "&")+"__hash="+escape(h);
location.replace(location.pathname+s);
}
};
})();
Run Code Online (Sandbox Code Playgroud)
假设服务器端没有任何东西使用$_GET['__hash'],这可以安全地使用。
| 归档时间: |
|
| 查看次数: |
1574 次 |
| 最近记录: |