Caching problem with asynchronous javascript loading with onload event?

Yes, this is fairly simple. Just give a random parameter to your URL, like : URL = yoururl.com -> yoururl.com/? Number=(random number) this way you will always have a unique url.

The parameter will be thrown away by the page when it is loaded because it is not used. Let me know if this helped.

1 ie: xx. Src = 'myserver.de/myjs.js?' + new Date().getTime(); – Mic Sep 9 '10 at 8:38 As Mic says, using the date is a good ID, I mostly work with Guids – Nealv Sep 9 '10 at 8:44 Thanks, that would work. But then it would never be cached at all and the script will later be used on websites with a huge amount on pageviews.So not caching at all is no real solution for me.

Maybe I go with on a per day base with "Date". I will still let it unaccepted for a bit, maybe somebody else comes up with another solution, but thanks. EDIT: lol took to long with my comment ;) – TheCandyMan Sep 9 '10 at 8:44.

A good way to solve this problem is to calculate the md5() of the file contents, and then append that value to the URL as a parameter. That way the file keeps getting cached as long as the file contents is the same. Another way is to control the caching behavior of the script with HTTP-headers, such as a ETag or lowering the maximum cache expiry time.

First solution is not possible for me, because the customers must integrate the script on their websites (this is just as template for them) and therefore if the js would change the customers must also change the md5. ETags are also set by the server. Strange thing is, if I look at the "Network" tab of Operas Dragonfly there isn't even a request done for the cached JS file, it seems it directly loading from cache without even checking against the server.

– TheCandyMan Sep 9 '10 at 12:59 @TheCandyMan666 Maybe that is because you have previously set some cache settings that tells the browser to load from cache instead of going to the server? That's the purpose of caching. – Emil Stenström Sep 13 '10 at 10:46 Maybe it has some default settings for this.

I mean, on the next day it reloaded the script, so in the end it isn't a big deal. But I think it should still always check against the server and wait for the (304? ) not modified response.

And if I did it without onload, it did check and recognize the changes. – TheCandyMan Sep 13 '10 at 11:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions