Detecting when CSS gets loaded by the browser with Mootools and Asset.css

Jan 27 2010 Published by Eneko Alonso under uncategorized

Asset.css claims to have an onload event in Mootools documentation, but sadly, not all browsers support this feature. Even worse, browsers like Chrome seem to support the event, but never fire it.

After some research and testing at work, we have come up with a solution that overrides Asset.css to detect when CSS gets loaded by the browser using the onload event in those browsers that support it (Internet Explorer and Opera) and doing polling in those that do not (Chrome, Safari and Firefox). Here is the code:

  1. // Add callback support to Asset.css for all browsers
  2. Asset.css = function(source, onLoad) {
  3.   var link = new Element('link', {
  4.     rel: 'stylesheet',
  5.     media: 'screen',
  6.     type: 'text/css',
  7.     href: source
  8.   });
  9.  
  10.   if (("onload" in link) && !Browser.Engines.webkit()) {
  11.     if (onLoad) link.onload = onLoad;
  12.   } else {
  13.     (function() {
  14.       try {
  15.         link.sheet.cssRules;
  16.       } catch (e) {
  17.         setTimeout(arguments.callee, 100);
  18.         return;
  19.       };
  20.       if (onLoad) onLoad();
  21.     })();
  22.   }
  23.  
  24.   link.inject(document.head);
  25.   return link;
  26. }
  27.  
  28. // Load some CSS and show an alert when done
  29. var mycss = new Asset.css('http://example.com/style.css', function() {
  30.   alert('CSS loaded!');
  31. });
  32.  
  33. // Unload the css
  34. mycss.destroy();

Enjoy!

No responses yet

Safari 4 rocks

Mar 01 2009 Published by Eneko Alonso under uncategorized

I’ve been using Safari both at home and work since last Thursday when I first knew it had been released and it looks very fast and solid. They have finally added some standard keyboard shortcuts, like Ctrl+Tab and now you can restore your previous session (all pages open) the next time you open the browser (although not automatically like Firefox).

It looks very good for web development too, since it has the Javascript console integrated and you can do direct changes on the DOM and the CSS to preview how it would look. But I think Firebug is still ahead and I’m very used to it, so I’m not ready to completely stop using Firefox.

I have nothing against Firefox, which has been my favorite browser for years, but the fact that it is very, very slow. I’ll say today is the slowest browser out there. Or at least it looks like that sometimes. The only extension I have is Firebug. I’ve tried removing the cache, the history, etc. with no luck.

So yeah, so far I’m very happy with Safari 4.

Safari 4

One response so far

Modifying the URL Hash without affecting the browser history

Dec 29 2008 Published by Eneko Alonso under uncategorized

Although this is not very common, some times you would like to modify the URL hash (this is the value after the # on the URL) without affecting the browser history. Let’s say you have multiple sets of tabs on your page or simply that your hash is going to change very often. But still want the user to be able to grab a permanent link to that page configuration.

The trick is to use the replace function, instead of directly assigning the value.

  1. // Direct assignment will add a new step to the history on browsers like Firefox 3
  2. window.location.hash = newHash;
  3. // Using the replace function changes the hash without affecting the browser history.
  4. window.location.replace(window.location.href.split('#')[0] + '#' +newHash);

Nice.

No responses yet