MooTools Asset.stylesheet onload event patch
A fully working fix for the onload event on the MooTools Asset.stylesheet library
The Asset.stylesheet library itself is a very useful library for preloading stylesheets via JavaScript. The only problem is that it does not support the onload event for all browsers. The reasoning behind this is because the onload event itself is not supported on all browsers; an older article posted on yearofmoo.com went into full detail about this problem. This blog article, however, provides the code for MooTools 1.2 and 1.3 that patches the Asset.stylesheet library to support the onload event 100% in all browsers.
Last Updated
This page was last updated on July 30th 2011 and was first published on July 30th 2011
Using the yearofmoo Asset.stylesheet patch any assets loaded with Asset.stylesheet will fire the onload event normally on success and will fire the onerror event on failure. The syntax is the same, here is an example:
new Asset.stylesheet('/path/to/some/css/file.css', {
onerror : function() {
alert('error file not loaded');
},
onload : function() {
alert('success');
}
});
What’s so special about this patch?
The good thing about this patch is that it uses the existing support of the onload event which is already supported by IE and Opera.It also does not use any other external requests to emulate the event. Finally, Asset.stylesheet comes with an onerror event which works with a timeout event (so once the request is checked enough times without success, it fires the onerror event). Finally, the patch itself works for stylesheet files that are located locally (on the same domain) and externally.
Demo
Here is a working demo of the Asset.stylesheet patch for MooTools:
Download
The Asset.stylesheet can be found on github.
Contact
Any issues or questions can be posted on the issue tracking page found on github.