Calling a js with Math.random() not working for IE7

Hi all,

I have a .js file with a variable inside. As this variable's value changes frequently I have to avoid cache by doing:

<script type="text/javascript" src="myfile.js?"+Math.random()></script>

This is working great for all browsers I've tested (even for IE6!!) but not for Internet Explorer 7 ...

Somebody can give me a clue on how to make this work for Internet Explorer 7 ??
Or a newer approach to try the above so IE7 gets convinced ..

Thanks a lot!
Who is Participating?
Michel PlungjanIT ExpertCommented:
Well if he was using php, he could just serve it as php

<script src="script.php"></script>
and put expires headers in the php and externalise the var that changes
Michel PlungjanIT ExpertCommented:
If it changes frequently then either take it out of the script or serve the script dynamically and set expiry headers on the server
To avoid caching, try this tag in html header

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">

Hope this help,
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Michel PlungjanIT ExpertCommented:
Why would that help not caching another file???
I'm not entirely convinced that this technique works at all.

Because you're not exactly inside the <script> tag, I have a hard time believing that "Math.random()" would even work.  Actually, the browser more than likely interprets it as random junk inside a tag with no attribute.  It would be the equivalent of:
<script type="text/javascript" src="myfile.js?" some random junk></script>

If you're using a scripting language, such as PHP, you could use it instead.

<script type="text/javascript" src="myfile.js?<?= mt_rand() ?>"></script>

Open in new window

Michel PlungjanIT ExpertCommented:
Well spotted

document.write('<script type="text/javascript" src="myfile.js?'+Math.random()+'"><\/script>')

would work too
@mplungjan, now that's just incestuous! =)
Dada44Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.