gaf*_*eck 5 javascript c# browser caching
有没有办法在Javascript或C#中判断某人正在使用的浏览器是否禁用了静态内容的缓存?
我需要能够测试浏览器是否针对缓存进行了优化.
更新
我对这个问题做了更多的调查,您可以在我最近的帖子中找到更详细的答案 。注意,下面描述的解决方案(最初)不是跨浏览器解决方案。
不确定它是否有帮助,但您可以尝试以下技巧: 1.向您的页面添加一些资源,假设它将是 javascript 文件cachedetect.js。2. 服务器应该cachedetect.js在每次有人请求时生成。并且它应该在响应中包含与缓存相关的标头,即如果浏览器的缓存启用,则资源应该被缓存很长时间。每个cachedetect.js应该看起来像这样:
var version = [incrementally generated number here];
var cacheEnabled; //will contain the result of our check
var cloneCallback;//function which will compare versions from two javascript files
function isCacheEnabled(){
if(!window.cloneCallback){
var currentVersion = version;//cache current version of the file
// request the same cachedetect.js by adding <script> tag dynamically to <header>
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "cachedetect.js";
// newly loaded cachedetect.js will execute the same function isCacheEnabled, so we need to prevent it from loading the script for third time by checking for cloneCallback existence
cloneCallback = function(){
// once file will be loaded, version variable will contain different from currentVersion value in case when cache is disabled
window.cacheEnabled = currentVersion == window.version;
};
head.appendChild(script);
} else {
window.cloneCallback();
}
}
isCacheEnabled();
Run Code Online (Sandbox Code Playgroud)
之后,您可以简单地检查一段时间cacheEnabled === true或之后。cacheEnabled === false