SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    caching of .js files

    I have a page that uses:

    src="/js/my_javascript.js" within a script tag to reference a javascript.

    My problem is that many browsers seem to be caching that .js file in a very stick manner. Even after a reboot, sometimes, it preserves the information contained in the cached version rather than downloading the most recent one.

    What steps can I take to ensure the most recent one is downloaded?

  2. #2
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, two questions:

    1) How often does the script change?

    2) Does your web server support server-side scripting (and if so, what)?
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  3. #3
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) Hmm, I'd say on average once a week, sometimes more sometimes less.

    2) Yes. PHP, Perl.

  4. #4
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then you have two options:

    1) Change your JS src to "/js/my_javascript.php", where that script is exactly the same as the original JS file except that it has a little bit of PHP at the start which adds appropriate HTTP headers for instant page expiry and/or no caching as well as setting the ContentType of the page to "text/javascript" (so that browsers aren't confused by the .php extension).

    2) Dynamically generate a random number and tack that on the end of the JS URL (using PHP scripting) so that the browser retrieves it every time (e.g. src="/js/my_javascript.js?random=936581"). The current date/time could be used instead for simplicity.

    Sorted?
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  5. #5
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!

  6. #6
    SitePoint Addict
    Join Date
    Feb 2001
    Posts
    302
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you go with Marco's second suggestion you could use Javascript rather than PHP and reduce the work for your server (albeit a small reduction).

  7. #7
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, but that would require javascript document.write 'ing the <script src="... tag, which I don't think works.

    So, I think PHP or something server side is necessary to write the <script src="... tag w/ the generated random number or date in order to ensure a different value and the .js file being retrieved.

  8. #8
    SitePoint Addict
    Join Date
    Feb 2001
    Posts
    302
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I was thinking of when you write a link to a stylesheet in javascript, I am obviously having a bad day.

  9. #9
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha... welcome to Monday. I just had my afternoon dose of caffiene. Back to the grind. thanks tho

  10. #10
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Krustage
    hmm, but that would require javascript document.write 'ing the <script src="... tag, which I don't think works.
    Actually, I suspect that it might work! However it's probably still easier to do it in PHP (negligible overhead really), and it's guaranteed to be 100% cross-browser!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  11. #11
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I don't think it does. As soon as the browser (at least IE) comes across </script> tag, even if it's in the middle of a document.write(... it will terminate the script.

    So,
    <script language="JavaScript">
    var theDate = new Date();
    document.write('<script language="JavaScript" src="/js/myjs.js?temp='+theDate.getTime()+'"></script>');
    </script>

    ... will break because the bolded </script> in there will terminate the original <script tag. And the document.write hadn't finished -- therefore generating a JS Error: "Unterminated String Constant".

    At least that's my experience. I'd love to hear otherwise
    Last edited by Krustage; Oct 21, 2002 at 12:04.

  12. #12
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha, just when I think I have something. This may solve the problem described in my previous post:

    <script language="JavaScript">
    var theDate = new Date();
    document.write('<script language="JavaScript" src="/js/myjs.js?temp='+theDate.getTime()+'">');
    </script></script>

  13. #13
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, no, no, no no!

    Surely you've come across this problem and workaround before?
    Code:
    <script language="JavaScript">
    var theDate = new Date();
    document.write('<' + 'script language="JavaScript" src="/js/myjs.js?temp='+theDate.getTime()+'"></' + 'script>');
    </script>
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  14. #14
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that is nicer. Thanks for the tutelage. Should have known better.

  15. #15
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!


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
  •