Javascript 用document.write()替换页面内容时如何避免在浏览器中缓存

Javascript 用document.write()替换页面内容时如何避免在浏览器中缓存,javascript,browser-cache,Javascript,Browser Cache,我在浏览器缓存方面有点困难 我有一个静态HTML页面(让我们称之为index.HTML),其中包含实现神奇功能的Java脚本(嵌入交互式服务)。这些脚本具有AJAX响应错误处理程序,如下所示: function(response){ // response is prototype.js AJAX response document.open("text/html"); document.write(response.responseText); document.close();

我在浏览器缓存方面有点困难

我有一个静态HTML页面(让我们称之为index.HTML),其中包含实现神奇功能的Java脚本(嵌入交互式服务)。这些脚本具有AJAX响应错误处理程序,如下所示:

function(response){ // response is prototype.js AJAX response 
  document.open("text/html");
  document.write(response.responseText);
  document.close();
}
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate, max-age=0" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Thu, 01 Jan 1970 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
var cacheBuster = '?buster=' + (new Date()).getTime(), //add a timestamp
    url = '/my/url' + cacheBuster;

new Ajax.Request(url, { 
    //... your ajax request 
});
当调用它并替换文档内容时,浏览器会缓存它(我不希望它被更改),而按F5什么也不做。Ofc。CTRL+F5工作正常,但我不能假设用户会这样做

这个函数是我得到的错误AJAX响应的处理程序。它用于将页面内容替换为由前端Apache HTTP服务器响应的自定义错误页面,该服务器具有到另一个Apache HTTP服务器的反向代理,该服务器同样具有到Tomcat的反向代理,为脚本提供服务并处理来自脚本的请求

前端Apache HTTP服务器在其配置错误页面覆盖和缓存控制头中包含index.html:

ProxyErrorOverride On
<Files index.html>
  Header set Cache-control "no-cache, no-store, max-age=0, must-revalidate"
  Header set Pragma "no-cache"
  ExpiresByType text/html "access plus 1 second"
</Files>
ProxyErrorOverride打开
标头设置缓存控制“无缓存,无存储,最大年龄=0,必须重新验证”
标题集Pragma“无缓存”
ExpiresByType text/html“访问加1秒”
所以对我来说,index.html的缓存似乎被关闭了。我假设您看到我正在使用mod_代理,mod_头,mod_过期

Error page已配备用于缓存控制的元标记(我知道,我知道它们不能被考虑;),可以说它的标题如下所示:

function(response){ // response is prototype.js AJAX response 
  document.open("text/html");
  document.write(response.responseText);
  document.close();
}
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate, max-age=0" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Thu, 01 Jan 1970 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
var cacheBuster = '?buster=' + (new Date()).getTime(), //add a timestamp
    url = '/my/url' + cacheBuster;

new Ajax.Request(url, { 
    //... your ajax request 
});

最后,我的场景大致如下:

function(response){ // response is prototype.js AJAX response 
  document.open("text/html");
  document.write(response.responseText);
  document.close();
}
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate, max-age=0" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Thu, 01 Jan 1970 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
var cacheBuster = '?buster=' + (new Date()).getTime(), //add a timestamp
    url = '/my/url' + cacheBuster;

new Ajax.Request(url, { 
    //... your ajax request 
});
  • 页面加载
  • 加载并调用Java脚本,magic开始(服务嵌入到指定的div中,并开始使用AJAX与服务器对话)
  • Tomcat/内部HTTP服务器关闭(我不知道软件的维护/升级)
  • 脚本获取AJAX错误响应(responseText是前端Apache HTTP服务器用其错误页面覆盖的自定义错误页面)
  • 页面内容将替换为自定义错误页面
  • 服务器从4。起来(甚至不起来,那么它将从不同的级别处理)
  • 用户点击F5页面已重新加载(但未加载)
  • 我可以在这里有点混乱,所以如果有什么是不清楚的或我应该提供更多的细节,请询问我会尽快提供更多的信息

    附言: 我认为我的方法可能根本错误,或者做了一些愚蠢的事情。如果有,请告诉我

    编辑:

    实际上,它发生在ff(16.0.2,17.0)和IE9上。Chrome、Opera和Safari只需重新加载页面而无需缓存。哦,更糟糕的是ff和ie只有在地址栏中按enter键时才在ctrl+f5上刷新:P


    在ff上我得到wycywig:///在documentURI属性中

    您正在使用IE吗?IE喜欢缓存AJAX get请求,而不管缓存头响应如何。您是否尝试过向请求URL添加一个缓存破坏字符串,如下所示:

    function(response){ // response is prototype.js AJAX response 
      document.open("text/html");
      document.write(response.responseText);
      document.close();
    }
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate, max-age=0" />
    <meta http-equiv="expires" content="0" />
    <meta http-equiv="expires" content="Thu, 01 Jan 1970 1:00:00 GMT" />
    <meta http-equiv="pragma" content="no-cache" />
    
    var cacheBuster = '?buster=' + (new Date()).getTime(), //add a timestamp
        url = '/my/url' + cacheBuster;
    
    new Ajax.Request(url, { 
        //... your ajax request 
    });
    

    实际上,这是在Firefox16.0.2上首次观察到的。另一方面,缓存AJAX响应是一个问题,但我替换了页面的全部内容,链接并没有改变。若我替换位置的搜索部分,它会自动重新加载页面。我们陷入了不同的境地。对我来说,这会导致页面重新加载。还有一件事:如果我的servlet reposnds404“还没有找到什么”,但服务器运行正常,我就会陷入重新加载页面的ifinite循环。PS:更改哈希部分对此不起作用(ie上也有iframe),我已经设法通过在这个“交互式服务”中实现带有i18n错误消息的错误框来开发解决方案。但我仍然热衷于如何在执行
    document.write
    时使用缓存解决这个问题。