Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force no cache using javascript and basic script loading

I have a bookmarklet that keeps using cache versions of http://www.imvu-e.com/products/hpv/download/HPV.js. I want it to never cache it and always reload it.

This is the hyperlink I use to save the bookmarklet (which users drag to browser toolbar that installs it):

<a href="javascript:(function(){
     c = document.createElement(&quot;script&quot;);
     c.type = &quot;text/javascript&quot;;
     c.src = &quot;http://www.imvu-e.com/products/hpv/download/HPV.js&quot;;

     c.onload = c.onreadystatechange = function()                    {
            if ( ! (d = this.readyState) || d == &quot;loaded&quot; || d == &quot;complete&quot;){
                document.documentElement.childNodes[0].removeChild(c);
                version='beta';
            }
     };
     document.documentElement.childNodes[0].appendChild(c);
})();">Run HPV</a>
like image 921
ParoX Avatar asked Feb 22 '12 17:02

ParoX


People also ask

How do I force a JavaScript file to load?

js and . css files. That is the reason those new changes will not appear to the user. The user either has to clear the browser cookie & reload the page or else he or she has to do a hard refresh of the page by pressing Ctrl+F5.

How do I stop file caching?

To Completely Disable Write-Behind CachingClick File System, and then click the Removable Disk tab. Click to clear the Enable write-behind caching on all removable disk drives option.


2 Answers

Add a useless querystring to the end of your url:

c.src = "http://www.imvu-e.com/products/hpv/download/HPV.js?" + (new Date).getTime(); 
like image 115
Prescott Avatar answered Sep 22 '22 02:09

Prescott


Use jQuery's getScript instead of a script tagging and altering the source

<script>
   $.getScript("JavascriptFileName.js?timestamp=" + new Date().getTime());
</script>
like image 23
ahankendi Avatar answered Sep 21 '22 02:09

ahankendi