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!

How to refresh a Javascript file in the browser's cache?

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,511
Scotland
Apoligies if this is not the correct forum for this question.

I am looking for a way to force a Javascript file in visitors' browser caches to be refreshed.

I have a site that contains many HTML pages, which is updated frequently. It has a small number of regular visitors. I understand the issues surrounding cached HTML pages, and this is not a problem. The visitors always see the latest version of each page, as you would expect.

But that is not the case with Javascript files. I'm finding that visitors are still being served old versions of JS files from their browser's cache, even when there is a new version on the server. I know that I can add an arbitrary parameter to the JS filename when calling it from HTML, like this:

[tt]
<script type="text/javascript" src="library.js[highlight #FCE94F]?n=1[/highlight]"></script>[/tt]

The [tt]?n=1[/tt] tells the browser that the file this is different from the cached version, and so the file is refreshed.

That work's OK. My problem is that I have the above line of code on every one of the site's pages, so I will have to apply the above change to each one in turn. I can do that, but I am wondering if there is some alternative way of achieving my goal that doesn't involve editing a large number of files.

Thanks in advance for any suggestions.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
[ol 1]
[li]You can configure most servers to not cache a particular filetype.[/li]
[li]You can use server-side or client-side scripting to make that "n" parameter pass the current time.[/li]
[/ol]

Mass find/replace should not be a challenge if the string is identical in all your HTML files.
 
Spamjim, thanks for your prompt reply, which was helpful.

I don't have enough control over the server to be able to configure it not to cache. But passing the current time as a parameter sounds promising.

That said, for now I think I'll just do the bulk find-and-replace. I use Notepad++, which has a "Replace all in open documents" feature, which should do the job nicely.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Notepad++ is great, of course. You might also look at grepWin, which can do the same find/replace task on closed files. GrepWin can also save backups of the original files (index.html.bak) so you never get yourself into too much trouble.

 
Yes, I know grepwin. It's very useful. I especially like the fact that it has a ocmmand-line interface.

I wasn't trying to avoid doing a bulk find-and-replace. I just wanted to be sure I wasn't missing some other approach to the problem. It's all done and dusted now.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top