How can I prevent an external javascript file from being cached?
any ideas?
Printable View
How can I prevent an external javascript file from being cached?
any ideas?
Try
Code:<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Cache-Control" content="no-cache" />
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 thismy html page reads this using the normal methodCode:var myArray=new Array;
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 ofCode:<script src="array.js"></script>
so I end up with a js file looking like thisCode:myArray[myArray.length]=(textarea.value)
I then use a setTimeout to keep reading the array.js usingCode:var myArray=new Array;
myArray[0]="message 1"
myArray[1]="another message"
myArray[2]="third message"
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.Code:function reloadJS() {
document.getElementsByTagName("script" )[0].src="array.js"
...........
..........
setTimeout("reloadJS()",100)
}
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.
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.
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 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.
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 :).Quote:
Originally Posted by Markdidj
Do you use a server-side language? Try adding a random query-string to the <script> src tag, like this:Use your server-side language to create the random number. The browser will reload it each time because it thinks the file is different.Code:<script language="javascript" type="text/javascript" src="somescript.js?rand=212293323"></script>
I Tried
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......Code:document.getElementsByTagName('script')[0].src="messages.js?message="+Math.random*1000
Vinnie, I don't know about JSP, do I need extra plugins? Any good pages that demonstrates its capabilities? Thanks.....
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.
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
I can give you an example in PHP of what I did in JSP:Quote:
Originally Posted by Markdidj
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;
}
Not much else to it really. :)HTML Code:<script type="text/javascript" src="javascript.php"></script>