Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  2] [ 3]  / answers: 1 / hits: 43171  / 12 Years ago, fri, january 25, 2013, 12:00:00

how to clear browser cache and history via javascript
I found some links like:-



How to clear browser history oan clear cache?



how to clear browser hash history in javascript



but they do not properly described on how this can be achieved via javascript ( or jQuery)



I require this so that the user do not have to do a [Ctrl+F5] or manually clear cache
and the most recent version of javascript file is loaded in the browser.



The main aim is to load the latest javascript files every time the user visits the website.
My application in ASP.NET MVC based and javascript files are included in .csHtml file.


More From » jquery

 Answers
28

There are other ways to prevent a JavaScript file (or other files) from being cached.



Simply append some dummy parameters to the end of the url. For example -



http://www.yourCoolSite.com/resources/js/main.js?r=SOME_RANDOM_VALUE


You would optimally do this on the server side. In PHP, this would look something like this -



echo http://www.yourCoolSite.com/resources/js/main.js?r=.time();


Using the time() function, we append the current epoch timestamp to the URL and therefore guarantee* that the file will appear different to the browser each time.



* Until 2038 that is ;)


[#80627] Thursday, January 24, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austynp

Total Points: 505
Total Questions: 118
Total Answers: 106

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
austynp questions
;