admin管理员组

文章数量:1299993

I have a list of js files, css and images which doesn't need to load from server every time, but if there is any update in files or bug fixes, only during that time I want to replace the files from browser cache, I know there is no access to browser cache, but is there any other ways to do so? My application will be used by specific users (known people), where I can install any program in their system, can anybody suggest me efficient way to do so? I don't want to load the files every time from server by setting 'no-cache'.

I have a list of js files, css and images which doesn't need to load from server every time, but if there is any update in files or bug fixes, only during that time I want to replace the files from browser cache, I know there is no access to browser cache, but is there any other ways to do so? My application will be used by specific users (known people), where I can install any program in their system, can anybody suggest me efficient way to do so? I don't want to load the files every time from server by setting 'no-cache'.

Share Improve this question asked Jul 14, 2012 at 19:19 suhas shettysuhas shetty 211 silver badge3 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

The most effective way to force the browser to refresh certain files at certain times is to add an arbitrary extra query string to the link:

<script type="text/javascript" src="http://mywebsite./js/scripttoload.js"></script>

then change to:

 <script type="text/javascript" src="http://mywebsite./js/scripttoload.js?V=2"></script>

Next time the page is requested the browser will think this is a new file. There are loads of other ways with headers etc but this works well

No, there isn't.

Javascript doesn't have access to the cache - the browser doesn't expose this information to the javascript engine.

A monly-used trick is to set the cache for the files to last for ages, so that they aren't requested again. However, when you want them to be updated, you can append a timestamp to the filename after a question mark. EG:

<link rel="stylesheet" href="style.css?123211212"/>

Every time the number changes, the browser thinks it's a different file and will re-download it. If the number doesn't change, then it uses the cached version.

What I do is, as part of the build process, rename all the statically referenced files to something involving their md5 hash. Then I set the headers so that they're cached for the max possible time. As soon as they change, they get a new name, so there's never an issue.

本文标签: