Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Caching Javascript .js files

Status
Not open for further replies.

rhowes

IS-IT--Management
Jun 6, 2001
140
ZA
Hi All

I have a problem with my browser (IE) caching .js files even when clearing my cache.

Has anyone else had this problem?
Is there an apache solution or do I have to find a source code/IE solution?

Thanks
Cheers

Richard
(Johannesburg, South Africa)
 
I have had this problem also. I think this is because
Code:
<SCRIPT SRC
files aren't treated as regular web pages by IE. I have found that even if you include the NOCACHE header, when serving .js files, IE happily ignores this.

Your best bet, if you really need to be sure your JS code is fresh, is to use server-side includes, or some other dynamic server-side method of including the script directly in the HTML page. This accomplishes the same goal as the external script source (one script included in many pages), but is handled by the server, rather than the browser.

Another trick you might try, though is to add a fake URL query string to your javascript source attribute. This would make it seem as if the browser is requesting a different file every time the query string changes, but in fact would not affect the download of the actual file at all:

Code:
<script src=&quot;myjsfile.js?fakevariable=randomtext&quot;>

Actually, this method of calling Javascript can be even more useful if you are using a server-side scripting language like PHP or ASP, because your external JS file can just as easily be a PHP-outputted page:

Code:
<script src=&quot;myjsfile.php?realvariable=value&quot;>

This allows your HTML page to request a certain external Javascript page with certain query string variables, which can be used in the output to dynamically affect what gets loaded. -------------------------------------------

Big Brother: &quot;War is Peace&quot; -- Big Business: &quot;Suspicion is Trust&quot;
(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top