• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

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!
0
Dada44
Asked:
Dada44
1 Solution
 
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
0
 
sompol_kiatkamolchaiCommented:
To avoid caching, try this tag in html header

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

Hope this help,
Sompol
0
 
Michel PlungjanIT ExpertCommented:
Why would that help not caching another file???
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
asafadisCommented:
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

0
 
Michel PlungjanIT ExpertCommented:
Well spotted

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

would work too
0
 
asafadisCommented:
@mplungjan, now that's just incestuous! =)
0
 
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
0
 
Dada44Author Commented:
thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now