×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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:


<script type="text/javascript" src="library.js?n=1"></script>


The ?n=1 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

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

  1. You can configure most servers to not cache a particular filetype.
  2. You can use server-side or client-side scripting to make that "n" parameter pass the current time.
Mass find/replace should not be a challenge if the string is identical in all your HTML files.

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

(OP)
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

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

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.

https://tools.stefankueng.com/grepWin.html

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

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close