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:
-
// Add callback support to Asset.css for all browsers
-
Asset.css = function(source, onLoad) {
-
var link = new Element('link', {
-
rel: 'stylesheet',
-
media: 'screen',
-
type: 'text/css',
-
href: source
-
});
-
-
if (("onload" in link) && !Browser.Engines.webkit()) {
-
if (onLoad) link.onload = onLoad;
-
} else {
-
(function() {
-
try {
-
link.sheet.cssRules;
-
} catch (e) {
-
setTimeout(arguments.callee, 100);
-
return;
-
};
-
if (onLoad) onLoad();
-
})();
-
}
-
-
link.inject(document.head);
-
return link;
-
}
-
-
// Load some CSS and show an alert when done
-
var mycss = new Asset.css('http://example.com/style.css', function() {
-
alert('CSS loaded!');
-
});
-
-
// Unload the css
-
mycss.destroy();
Enjoy!
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.

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.
-
// Direct assignment will add a new step to the history on browsers like Firefox 3
-
window.location.hash = newHash;
-
// Using the replace function changes the hash without affecting the browser history.
-
window.location.replace(window.location.href.split('#')[0] + '#' +newHash);
Nice.