www.webdeveloper.com
Results 1 to 4 of 4

Thread: how to force no-cache in scripts

  1. #1
    Join Date
    Dec 2012
    Posts
    7

    how to force no-cache in scripts

    HI,
    I've got a little question here. Is there any chance of forcing a browser not to cache a js script? My page is in HTML and I can clear the cache on page load every time, but some of the sections would stop working correctly. The script is below. It's a simple cash converter and current exchange rates I've found, but it's not updating unless I will hot Ctrl+F5.

    Code:
    <script type="text/javascript" src="http://widget.fx-exchange.com/converter.php?fg=en&ff=GBP&ft=USD,EUR,CHF,PLN,&fa=1&cb=F0F0F0&fy=3"></script>
    Code:
    <script type="text/javascript" src="http://widget.fx-exchange.com/converter.php?fg=en&ff=GBP&ft=PLN&fa=100&cb=F0F0F0&fy=1"></script>

  2. #2
    Join Date
    Mar 2009
    Posts
    431
    Since the script you are downloading is generated by a PHP program you might try having the PHP script to send an "expires" header with a date set to sometime in the past.

    I don't know if that will work or not. A "no-cache" header is ignored by certain browsers so you can't depend on that.

  3. #3
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    You could append a random number/string to the end of the script URL:

    Code:
    <script type="text/javascript" src="yourScript.php?param=foo&<?php echo rand(0,100000); ?>"></script>
    This won't stop the caching but it should load the script every time (because the URL is always a new one).

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    alll browsers WILL not cache a url if ALL of the following are true:

    - the response has an Expires header in the past, present, or very-near future
    - the response has a Date header
    - the response status code is 200
    - certain proxies want Cache-Control: Private to avoid local caching.

    if a 304 slips out, the browser gets paranoid and usually pings the script url going forward.
    at that point, if your Date and Expires are different, or the content-length has changed, and yet expires is in the past, a new copy should be shipped.

    you can bank on that algo by avoiding 304s altogether.

    other things like etags, pragma, can help good caching, but don't really work to prevent caching.

    the url creeping of using a random() or timestamp in the url pollutes the cache and purges useful content, don't do it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles