0

在阅读了几个线程之后,我遇到了两个基本的解决方案。

一个涉及要求浏览器不要缓存 - > 使用 HTML 元标记 S1:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> 
<meta http-equiv='expires' content="-1"/>
<meta http-equiv='pragma' content="no-cache"/>

它对我不起作用。

其他是在 URL 后面放随机数

S2:

<script language="javascript" src="./js/myJS.js?'+ Math.Random()*100+'"/>

这些适用于 Chrome 和 Mozilla,但由于某种原因,它们不适用于我在 IE 上。

有人知道另一种方法吗?

我无法将 S2 嵌入到脚本块中。如果可以做到,那么这可能会奏效。

<script> 
document.write("<script language=\"javascript\" src=\"./js/myJS.js?'+ Math.Random()*100+'\"/>") 
</script>
4

3 回答 3

1

您的解决方案 2 是正确的方法,但您不能在 <script> 标记内写入字符串“<script/>”,因为它会混淆 HTML 解析器。有关更多信息,请参阅此问题

您可以使用以下技巧来规避此问题:

<script> 
<![CDATA[document.write("<script language=\"javascript\" src=\"./jsdir/RS_01_eng.js?'+ Math.Random()*100+'\"><\/script>")]]>
</script>

我还建议您删除 language="javascript" 因为它在 HTML5 中不再需要。

另外,也许您应该从Math.Random() * 100切换到new Date().getTime()以获得更独特的东西并防止一百分之一的边缘情况。

于 2013-10-25T12:09:29.637 回答
1

您是否尝试过转义脚本标签?

<script >
    document.write(unescape("%3Cscript src='./js/myJS.js?r=" + Math.random().toString().replace('.', '') + "' type='text/javascript'%3E%3C/script%3E"));
</script>
于 2013-10-25T13:08:42.800 回答
0

你有没有尝试过:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

答案来自:使用 <meta> 标签关闭所有浏览器中的缓存?

编辑:我知道你已经尝试过一些元标签,但上面写的类似标签过去对我有用。

于 2013-10-25T12:08:29.167 回答