2017-02-24 88 views
2

我的服务器向客户端发送包含许多组件的页面。 我所有的图片,js和css都被缓存。无缓存重新验证的重新加载页面

在我的网页上,有一个JavaScript这样的:

function refresh(){ 
    window.location.reload(); 
} 
var timer = setInterval("refresh()", 60000); 

其实,我需要经常刷新我的网页,以验证是否有我的服务器上的新信息。

但是,问题是:当我重新加载我的页面时,我的所有组件(图片,css,js ...)都被重新验证。我想检查我的信息,但不是所有组件。

所以,我想知道是否有可能重新验证只有我的网页的一部分,没有图片,JS和CSS重新确认

感谢

+0

当你说我的更新页面的一部分,你说的是一些文件或HTML或精确 –

+0

其实什么,我重装我的网页,并与新的信息获取HTML。此页面包含缓存的图片,js和css。我只是想获取我的HTML并使用缓存而不是重新验证所有组件 –

回答

0

尝试使用AJAX加载代替:仅加载新来自服务器的信息。 而且,自动重新加载页面对用户来说非常烦人。在JS,CSS喜欢的网址的末尾

0

添加查询随机值,

<script src="a.js?timestamp=1234567"> 

并改变每个页面加载的时间戳值。

+0

此解决方案与我的目标相反,不是吗?我想使用更多的缓存,而不是避免它。 –

0
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 
+2

请给出你正在提议的不同标题的解释。只要把它们放在OP上,就会有随机复制和粘贴以及货运结果的风险。 – filmor

+0

这是我对png的响应标题:Cache-Control:public,must-revalidate,max-age = 7200。所以,如果我不到2个小时后回到我的页面,我将使用我的缓存。但是,如果我在一分钟后自动重新加载页面,我将在使用它之前重新验证缓存 –

0
You can use local storage to check the page first time or reload then avoid validate in document loading. 

// Check browser support 
if (typeof(Storage) !== "undefined") { 
// Store 
localStorage.setItem("storage", "reload"); 
// Retrieve 
if(localStorage.getItem("reload") == "reload") 
{ 
var isReload = true; 
} 
} 
if(!isReload) 
{ 
    //validation logic 
} 

function refresh(){ 
window.location.reload(); 
} 
var timer = setInterval("refresh()", 60000); 


//you have to clear the storage once you are logging out or leaving 
function removeloacalstorage(name) 
{ 
    //here name is "storage" 
    localStorage.removeItem(name); 
} 
相关问题