Results 1 to 2 of 2

Thread: Prevent the caching of .js files

  1. #1
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Prevent the caching of .js files

    Hi

    Sorry if this has been covered here before - I did a quick forum search but didn't find the right answer.

    Q. With an external .js file, where the filename remains constant but its contents vary over time, is it possible to ensure that the web browser always loads the current website version, rather than a cached copy?

    I thought of using the pragma: no-cache HTML header but I'd rather not as I want images to be cached to ease page loading.

    Thanks for your help...

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript" src="whatever.js"></script>
    <script type="text/javascript">
    (function(s){
    	s = s[s.length - 2];
    	s.src = s.src + '?bustcache=' + new Date().getTime();
    })(document.getElementsByTagName('script'));
    </script>
    Or, a server side language could be used if available, ex with PHP:

    Code:
    <script type="text/javascript" src="whatever.php"></script>
    Just have a javascript and a no-cache header in the whatever.php file. The rest could be ordinary javascirpt.
    Last edited by jscheuer1; 01-13-2010 at 04:55 PM. Reason: add bit about PHP
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •