SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    javascript stop cache

    How can I prevent an external javascript file from being cached?
    any ideas?
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  2. #2
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Try
    Code:
    	<meta http-equiv="Pragma" content="no-cache" />
    	<meta http-equiv="Expires" content="-1" />
    	<meta http-equiv="Cache-Control" content="no-cache" />
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  3. #3
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but it didn't work with what I've been trying to do. I'm trying to save a variable to an external js using server side then reading it again client side. I've started a js file with this
    Code:
    var myArray=new Array;
    my html page reads this using the normal method
    Code:
    <script src="array.js"></script>
    and reads the contents of myArray, which starts of empty. I import the length of the array to a hidden input, and create a textarea, and the form is sent to the server program which adds it to the js in the form of
    Code:
    myArray[myArray.length]=(textarea.value)
    so I end up with a js file looking like this
    Code:
    var myArray=new Array;
    myArray[0]="message 1"
    myArray[1]="another message"
    myArray[2]="third message"
    I then use a setTimeout to keep reading the array.js using
    Code:
    function reloadJS() {
    document.getElementsByTagName("script" )[0].src="array.js"
    ...........
    ..........
    setTimeout("reloadJS()",100)
    }
    It works on my computer (now with apache) but when I try to run it online it doesn't update the new variables, but it does load the js. I added the lines you suggested but it continues using stored values as if from a cached javascript file.
    Is there any other way of reloading a javascript file?

    ps I know 'document.getElementsByTagName("script" )[0].src="array.js"' only works in IE, but it seems it could be a fast option.
    Last edited by Markdidj; Sep 13, 2003 at 13:22.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  4. #4
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll need to send a no-cache header with the js file.

    I don't know what server your host is running, so I can't help you any more than that.
    Love it? Hate it? Helpful? Useless?
    Use the rate button to let me know what you think of my post!

  5. #5
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    can javascript files have headers? or is that to do with perl ? ( thats what I use). I dont want to make a html file containing js, it has to be pure js.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  6. #6
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I heard quite a while ago that javascript can be run from server. Is that possible? That will stop it from being cached, but I can't find the info on it now.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Markdidj
    can javascript files have headers? or is that to do with perl ? ( thats what I use). I dont want to make a html file containing js, it has to be pure js.
    I actually had to do the same thing today. I ended up making it a JSP file and sending no-cache headers using server-side code. You can do the same in the server-side language of your choice. However, if there's another solution to this I'd love to hear it .

  8. #8
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you use a server-side language? Try adding a random query-string to the <script> src tag, like this:
    Code:
    <script language="javascript" type="text/javascript" src="somescript.js?rand=212293323"></script>
    Use your server-side language to create the random number. The browser will reload it each time because it thinks the file is different.
    Of course, that's just my opinion. I could be wrong.

  9. #9
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I Tried
    Code:
    document.getElementsByTagName('script')[0].src="messages.js?message="+Math.random*1000
    and other attempts but it won't load the new js until the page has been refreshed. I'm trying to avoid rewritting the page server-side or refreshing the page, and I'm trying not to use xml either as that would mean rewritting javascript variables to xml, saving server-side, importing, then turning back to js variables. seems long winded......
    Vinnie, I don't know about JSP, do I need extra plugins? Any good pages that demonstrates its capabilities? Thanks.....
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  10. #10
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you adverse to using server-side scripting to “rewrite” the page that then loads your script? You can’t add a random value in the way I described using client-side scripting; it must be server-side.
    Of course, that's just my opinion. I could be wrong.

  11. #11
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It just seems long winded when I already have the page that I want loaded. It just means I have to get the perl script to do more work, when I only want it to save 1 variable. My thought was to do as much client-side as possible, so more people will be able to use the perl script at the same time.

    I created a simple chatroom using this method (works on my computer but not online), and thought that it would also be useful for games
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  12. #12
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Markdidj
    I Tried
    Code:
    document.getElementsByTagName('script')[0].src="messages.js?message="+Math.random*1000
    and other attempts but it won't load the new js until the page has been refreshed. I'm trying to avoid rewritting the page server-side or refreshing the page, and I'm trying not to use xml either as that would mean rewritting javascript variables to xml, saving server-side, importing, then turning back to js variables. seems long winded......
    Vinnie, I don't know about JSP, do I need extra plugins? Any good pages that demonstrates its capabilities? Thanks.....
    I can give you an example in PHP of what I did in JSP:
    PHP Code:
    <?php
    //tell browser not to cache this page
    header("Cache-Control: no-store, no-cache, must-revalidate"); 
    header("Cache-Control: post-check=0, pre-check=0"false); 
    //tell the server this is javascript content.
    header("Content-type: text/javascript");
    ?>
    //insert javascript code here
    function foo(bar) {
      return;
    }
    HTML Code:
    <script type="text/javascript" src="javascript.php"></script>
    Not much else to it really.


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
  •