views:

118

answers:

2

Hi guys, I am currently trying to load some js files asynchronously, so that they are not able to block the rest of the website. I mainly followed the descriptions found here:

Asynchronous Javascript

In terms of the non blocking loading of the javascript file this works great, but i got now the problem that the javascript file is cached and stays cached even if i change the content (also doing shift-reload does not help anything).

My current code of loading the script looks like the following:

 (function() {
   function xx_async_load() {
     var xx = document.createElement('script');
     xx.type = 'text/javascript';
     xx.async = true;
     xx.src = 'http://myserver.de/myjs.js';
     var el = document.getElementsByTagName('script')[0];
     el.parentNode.insertBefore(xx, el);
   }

   if (window.addEventListener) {
     window.addEventListener('load', xx_async_load, false);
   } else if (window.attachEvent){
     window.attachEvent('onload', xx_async_load);
   }

 })();

If i call the code inside "xx_async_load" directly and change the myjs.js, the changes are getting recognized, but if I am loading this through the onload event it always stays cached and the changed are never recognized.

Does anybody know a solution how I make the browser to recognize the changes in the cached files (problem appears in Opera, FF and IE work fine)?

EDIT: If i look at the "Network" tab of Operas Dragonfly, there isn't even a request done on reload for the cached JS file, it seems that it is directly loading it from cache without even checking against the file on the server.

EDIT2: I will test how long it stays in the cache. If its gone till tomorrow its fine. Else I can still propose the workaround with a date param (so accepting that answer). Thx again.

+2  A: 

Yes, this is fairly simple.

Just give a random parameter to your URL, like : URL = http://www.yoururl.com -> http://www.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

Nealv
ie: `xx.src = 'http://myserver.de/myjs.js?' + new Date().getTime();`
Mic
As Mic says, using the date is a good ID, I mostly work with Guids
Nealv
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 ;)
TheCandyMan666
+1  A: 

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.

Emil Stenström
Hi, 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.
TheCandyMan666
@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
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.
TheCandyMan666