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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

included .js file keep cashing 2

Status
Not open for further replies.

thepain009

Technical User
Oct 22, 2005
42
US
Hello pplz,

Here is my problem:

I have an HTML page in which I'm including a .js file with a single document.write() statement.

When the .js file is changed (by an outside php script), user needs to click on the 'refresh' button so see the changes.

Is there a way for the browser not to cache the js file, or to automatically refresh itself once?

Thank you in advance
 
There are two issues here. The first is how to get the page to reload itself (easy):

Code:
location.reload(true);

The "true" parameter tells the browser to get the latest versions of files on the page, rather than any cached versions. If that still doesn't work for you, address the second issue with some cachebusting params (given you're using document.write, that's also easy):

Code:
document.write('<script type="text/javascript" src="[URL unfurl="true"]http://whatever/somefile.js[/URL][!]?randParam=' + new Date.getTime() + '[/!]"></script>';

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thank you for your response! Both ideas make sense, but here are my problems with both:
1. First one does what I need, but is there a way to stop it from refreshing infinitively?

2. I tried this method using PHP and it worked like a charm. Nice trick. However, on one of the pages I dont have PHP available to i have to use javascript, but ur code doest seem to work.
If you close the parenthesis at the end, the script only prints out ); to the page. Are you sure that the script inside of the script will work?
 
1. It doesn't - it only refreshes once. Of course, if you're running it at the top of a page automatically instead of on some condition, of course it will refresh indefinitely. You'd have to use cookies or a parameter to detect you've refreshed, and to not do it any more.

2. The presence or absence of PHP has no relevance on this script - it's pure client-side JavaScript only.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top