Gaurav Wadhwani
Gaurav Wadhwani

Reputation: 378

How to defeat browser caching in IE?

After reading several threads, there are two basic solutions I have come across.

One involves asking browser not to cache - > Using HTML meta tags 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"/>

It didn't work for me.

Other was to put random number after the URL

S2:

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

These work on Chrome and Mozilla but for some reason they aren't working for me on IE.

Does someone know an alternative way?

I can't get to embed S2 into script block. If it can be done then this might work.

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

Upvotes: 0

Views: 595

Answers (3)

Rudy
Rudy

Reputation: 2353

Have you tried escaping the script tags?

<script >
    document.write(unescape("%3Cscript src='./js/myJS.js?r=" + Math.random().toString().replace('.', '') + "' type='text/javascript'%3E%3C/script%3E"));
</script>

Upvotes: 1

hazerd
hazerd

Reputation: 592

Your solution 2 is the right way to do it, but you can't write the string "<script/>" inside a <script> tag as it confuses the HTML parser. See this question for more info.

You can circumvent this problem with the following trick :

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

I also suggest that you drop the language="javascript" as it is no longer necessary in HTML5.

Also maybe you should switch from Math.Random() * 100 to new Date().getTime() in order to have something more unique and prevent the 1 in a hundred time edge case.

Upvotes: 1

Franky W.
Franky W.

Reputation: 188

Have you tried:

<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" />

Answer taken from: Using <meta> tags to turn off caching in all browsers?

Edit: I know you've tried some meta-tags, but similar tags as written above have worked for me in the past.

Upvotes: 0

Related Questions