gpt4 book ai didi

c# - 如何检查浏览器缓存是否被禁用

转载 作者:搜寻专家 更新时间:2023-11-01 04:33:29 24 4
gpt4 key购买 nike

在 Javascript 或 C# 中是否有一种方法可以判断某人使用的浏览器是否已禁用静态内容缓存?

我需要能够测试浏览器是否针对缓存进行了优化。

最佳答案

更新

我对问题做了更多调查,您可以找到更详细的信息 answer in my recent post请注意,下面描述的解决方案(最初)不是跨浏览器解决方案。

不确定是否有帮助,但您可以尝试以下技巧: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();

之后您可以在一段时间后简单地检查 cacheEnabled === truecacheEnabled === false

关于c# - 如何检查浏览器缓存是否被禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9203509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com