Saturday, July 21, 2012

How to prevent browser caching for css / javascript

Now a days i have an online shopping site for maintenance.
https://www.izabellondon.com
Since i have done lots of changes in the site, there are situations when we upload new css or javascript but client can not see the new changes because by default browser loads the css or javascript file from its cache memory if the URL is repeated. 


Then we have to ask our client to please clear your browser cache to get new changes. I am sure many of web developer has to face this situation then i decided not to ask our client for same thing.


I start wondering how to overcome this problem? after some google search i found the solution.


You need to add any random querystring after your call to javascript or css includes. for example.


CSS
<link href="/App_Templates/FrontEnd/Login.css?date=2012-07-02" rel="stylesheet" type="text/css" />


Javascript
<script src="/Content/swfobject.js?version=2" type="text/javascript"></script>


It will resolve the issue and your client will not need to clear the cache to get new changes. Querystring in css/javascript call will always load file from the server instead of browser cache.


:)


wow


Happy Coding :)