Refresh Javascript Sources

Discussion in 'Javascript' started by JamesBond, Nov 20, 2005.

  1. JamesBond

    JamesBond Guest

    I am using a javascript in my html page which will be updated daily.
    However, the javascript is getting cached by the browser and whenever
    the user visits my html page, he is getting a cached copy of the
    javascript though the javascript is updated.

    Is there any way to force the browser not to cache this javascript?
     
    JamesBond, Nov 20, 2005
    #1
    1. Advertising

  2. JamesBond wrote:
    > I am using a javascript in my html page which will be
    > updated daily. However, the javascript is getting cached
    > by the browser and whenever the user visits my html page,
    > he is getting a cached copy of the javascript though the
    > javascript is updated.
    >
    > Is there any way to force the browser not to cache this
    > javascript?


    That would be silly. You want the javascript cached as much as possible.
    (You want everything cached as much as possible). If you are changing
    the script on a daily bases you should send an HTTP Expires header than
    tells the browser when the current version expires and then it should
    check for a new version when the old one expires.

    Alternatively you could put a (dummy) query string in the SRC attribute
    for the javascript file and change that whenever you want a new version
    recovered from the server. I.E. you might start with:-

    SRC="someJsFile.js?V=1"

    - and when you update the JS file change the references to:-

    SRC="someJsFile.js?V=2"

    - and the browser will not see its cached version as the resource in
    question and it will have to get a new one from the server. That way the
    file is still cached on the client whenever it is not changed.

    Obviously going through an entire web site updating query stings is not
    a good idea so this would be better done dynamically, with the version
    number that is appended to the query string available as an application
    wide variable. This works best for intermittent and unpredictable
    updates, if you are genuinely updating daily then appropriate Expires
    headers are a much better approach.

    Richard.
     
    Richard Cornford, Nov 20, 2005
    #2
    1. Advertising

  3. JamesBond

    JamesBond Guest

    Hi Richard,

    Thanks for your reply. But, could you please let me know how I could
    sent Expires header in a javascript. I am very new to this.

    Thanks,
    James.
     
    JamesBond, Nov 21, 2005
    #3
  4. JamesBond wrote:

    > Thanks for your reply. But, could you please let me know how I could
    > sent Expires header in a javascript. I am very new to this.


    That header can only be set/sent from server-side, for example through
    JScript in ASP. Googling for "iis response header expires" linked to
    <URL:http://support.microsoft.com/kb/q234067/> as first hit.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 21, 2005
    #4
  5. JamesBond

    JamesBond Guest

    Is it possible that I call a php page which returns javascript content
    from the javascript tag as shown in the example below?
    <script language="javascript" src="/js.php"></script>

    In such a case, will the browser still cache it??
     
    JamesBond, Nov 22, 2005
    #5
  6. JamesBond wrote:

    > Is it possible that I call a php page which returns javascript
    > content from the javascript tag as shown in the example below?
    > <script language="javascript" src="/js.php"></script>


    Yes, however that should be

    <script type="text/javascript" src="/js.php"></script>

    See also news:

    > In such a case, will the browser still cache it??


    See <URL:http://www.mnot.net/cache_docs/>.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 22, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hamed
    Replies:
    3
    Views:
    3,350
    Richard Grimes [MVP]
    Feb 15, 2004
  2. brian lanning
    Replies:
    0
    Views:
    611
    brian lanning
    Jul 29, 2003
  3. Per Magnus L?vold
    Replies:
    1
    Views:
    5,166
    John C. Bollinger
    Oct 8, 2004
  4. Pkenty
    Replies:
    0
    Views:
    412
    Pkenty
    May 28, 2004
  5. Jawahar Rajan
    Replies:
    1
    Views:
    303
    Adrienne
    Oct 1, 2003
Loading...

Share This Page