0

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>
Gaurav Wadhwani
  • 358
  • 2
  • 6
  • 16

3 Answers3

1

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.

Community
  • 1
  • 1
hazerd
  • 592
  • 4
  • 6
  • Thanks, I tried it but didn't work. Maybe because i need to embed this in XSL ...... It says ERROR: 'Element type "scr" must be followed by either attribute specifications, ">" or "/>".' – Gaurav Wadhwani Oct 25 '13 at 12:21
  • Didn't work as in the cache is still there or the script wasn't loaded? – hazerd Oct 25 '13 at 12:25
  • Script didnt run. The XSL gave the compilation error and above is the reason for it. It needs closing of scr. You did fool JS with that :) but XSL isnt. – Gaurav Wadhwani Oct 25 '13 at 12:41
  • Ha ! I didn't notice XSL was involved. I added CDATA tags to counteract the XSL processor. That should do the trick. – hazerd Oct 25 '13 at 12:44
  • That does the trick for XSL ! but sadly my problem isn't solved. It still shows the cached copy. But I think you should edit your answer and show both the solutions, it could help someone using JS. – Gaurav Wadhwani Oct 25 '13 at 13:03
  • Try to do a hard-refresh (CTRL+F5) just once and test again. Maybe the old code is still in there. Also, what webserver are you using ? How does your file change exactly ? – hazerd Oct 25 '13 at 13:09
  • I can do hard-refresh but cannot enforce this on the customers. Weblogic. Rudy's method worked for me. I thought it was due to replacing decimal. So I tested your solution with Math.Random().toString().replace('.', ''). Still it shows old cached copy. – Gaurav Wadhwani Oct 28 '13 at 05:09
1

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>
Rudy
  • 2,323
  • 1
  • 21
  • 23
  • Thank you Rudy, it worked! I guess, the encoding fools the HTML parser into believing that the characters are usual string. – Gaurav Wadhwani Oct 28 '13 at 05:12
  • This approach is not working with CSS style-sheets. was changed to – Gaurav Wadhwani Oct 28 '13 at 07:24
0

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.

Community
  • 1
  • 1
Franky W.
  • 178
  • 3
  • 17