Preventing Cache on JavaScript files

18,023

The <meta http-equiv="Cache-control" content="NO-CACHE">, the directive CACHE-CONTROL:NO-CACHE indicates cached information should not be used and instead requests should be forwarded to the origin server.

In order to prevent cache on every request, you may need to add some random string in url. The example below is use javascript to dynamic create a script tag and adding random number in the url, then append it.

<script language="JavaScript">
 var s=document.getElementsByTagName('script')[0];
 var sc=document.createElement('script');
 sc.type='text/javascript';
 sc.async=true;
 sc.src='http://192.168.0.149/redirect.js?v' + Math.random(); 
 s.parentNode.insertBefore(sc,s);
</script>

If just want to prevent 1 time only, just append some string to the src.

<script src="http://192.168.0.149/redirect.js?12345678"></script>
Share:
18,023
Ryan Fitzgerald
Author by

Ryan Fitzgerald

I hate people that downvote. No question is ever a bad question.

Updated on June 17, 2022

Comments

  • Ryan Fitzgerald
    Ryan Fitzgerald almost 2 years

    I'm trying to prevent 2 JavaScript files from being cached by the browser.

    I've tryed to use <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> without success. Here's my <head> element code:

        <head>
    
    <meta charset="UTF-8">
    <meta http-equiv="Cache-control" content="NO-CACHE">
    
    <link type='text/css' href='/files/theme/popup_basic.css' rel='stylesheet' media='screen' />
    
    <!-- JavaScript Start -->
    <script type="text/javascript" src="/files/theme/gohome.js"></script>
    <script type="text/javascript" src="http://192.168.0.149/redirect.js"></script>
    <!-- JavaScript End -->
    
    </head>
    

    From my understating, this should work. But the redirect.js file keeps being cached!

    Anyone know what I'm doing wrong?

  • Jace Cotton
    Jace Cotton about 11 years
    You don't have to generate a random number, it can be a statically typed number using pure HTML. (See my answer).
  • Derek
    Derek about 11 years
    I'm considering the case that want to prevent the file cached on every request.
  • Ryan Fitzgerald
    Ryan Fitzgerald about 11 years
    Well I've tried static numbers before and they don't work. That's why I normally use PHP to use the date. Random numbers is really what you need. (I'm guessing.)
  • Derek
    Derek about 11 years
    So, you know how to use date.Random numbers to add random string to the src, then what are you seeking? The case above, I'm assuming you are not going to use any server side language.
  • Ryan Fitzgerald
    Ryan Fitzgerald about 11 years
    Well. I can't use a server side language. That's why I was forced into JS. I just didn't know how to do this! This works great. And was exactly what I was looking for. Thanks so much!
  • Derek
    Derek about 11 years
    OK, you are welcome. It is better to show the constraint in the question next time :)
  • Stealth Rabbi
    Stealth Rabbi about 6 years
    @JaceCotton WHere is your answer you're referring to?