Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

The advantage of including js on web page is that I need not to worry about cached js file. If I change it on the server, the same will be reflected on client side. But it increases the size and complexity of webpage. It also affects average response time.

But if I make a separate js, so it could be saved at client side on first request, client needs to remove cached js when I do some changes in js at server side.

Is there any way (some HTML tag or attribute) to indicate to the browser that js file has been changed at server side. So it downloads the new copy. Or something like versioning.

share|improve this question
    
HTML5 provides a good way to handle it using manifest –  articlestack Feb 7 at 14:50
add comment

3 Answers

up vote 6 down vote accepted

Use a made up query string attached to the end of the file name. Browsers think the file is new and download it. The query string does not affect the file.

So <script type="text/javascript" src="your.js?123"></script>

share|improve this answer
    
This is probably the easiest solution, although I keep hearing reports that certain browsers and proxies don't cache files with query strings in them. –  DisgruntledGoat Jun 15 '11 at 11:53
    
I am satisfied with @Jason Gennaro and @Bob Baddeley answers. I would like to add one more point which came in my mind after a doubt. If a js is being referred from mass number of webpages then changes on every page would not be possible. In such a case, Create a separate tile/templet/master page which have all common resources and settings. So the changes at one place would be applicable to all places. –  articlestack Jun 15 '11 at 15:46
add comment

This is what version numbers are good for. If you include your file with a version number in the file name (like jquery-1-5-1.min.js), then later when a new version of the file is deployed you update with the new version number (like jquery-1-6-0.min.js), then you don't have to worry about the caching.

share|improve this answer
    
I am confused whom answer should i accept/mark. Lets wait for sometime.. –  articlestack Jun 15 '11 at 15:50
add comment
<script>

    var ts = new Date().valueOf();//creating timestamp
    var newScript = document.createElement('script'); //creating script tag
    newScript.type = 'text/javascript';
    newScript.src = 'jquery-1.3.2.min.js?'+ts;// reference to jS with timestamp as a query string
    document.body.appendChild(newScript);//appending it to dom
    </script>

Add this script tag to body. Every time JS will be updated a fresh file.

share|improve this answer
    
But this means the file will be downloaded on every single page load! That's incredibly inefficient. –  DisgruntledGoat Jun 15 '11 at 11:52
    
@DisgruntledGoat is correct. There would be no sense for caching the cache as i mentioned in 2nd paragraph of my question. –  articlestack Jun 15 '11 at 15:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.