7

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>
2
  • pretty bad mix of js / html.. use a js framework Commented Feb 22, 2012 at 17:32
  • 2
    @yes123 It's a bookmarklet, and you're supposed to put a decent chunk of js in the url (because it needs to carry out the loading of your more complex js files) Commented Feb 22, 2012 at 17:57

3 Answers 3

19

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(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Had to use c.src = "http://www.imvu-e.com/products/hpv/download/HPV.js?" + new Date().getTime();
@NiCkNewman Date.now() maybe faster, but it doesn't work in IE6 ;]
2

Pure JS solution:

<script> 
var scr = document.createElement("script");
scr.src = "http://example.com/cool.js" + "?ts=" + new Date().getTime();
document.getElementsByTagName("head")[0].appendChild(scr);
</script>

Comments

1

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

<script>
   $.getScript("JavascriptFileName.js?timestamp=" + new Date().getTime());
</script>

1 Comment

I'm writing as an SO reviewer because your answer has been flagged for its quality. Please be aware when answering that your answers could be read many times in future. This makes it important to write explanatory notes in them. Would you please take a moment or two to edit your answer with some text that says how and why your answer provides a solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.