Source on Github: http://github.com/tschellenbach/Fashiolista-Button/blob/master/buttons.js
Blog post explaining the code http://www.mellowmorning.com/2010/08/03/creating-your-own-diggfacebook-liketweetmeme-button/
The current dom load version is rather verbose.
I need the domload event because the code detects all a[href]
elements in the page.
prior to domload these elements are not always available (depending on your browser).
Does anyone know a more elegant version or a way to avoid the need for the domload event?
Ready method:
ready: function() {
// Make sure that the DOM is not already loaded
if ( !fashiolistaUtils.isReady ) {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if ( !document.body ) {
return setTimeout( fashiolistaUtils.ready, 13 );
}
// Remember that the DOM is ready
fashiolistaUtils.isReady = true;
//start the fun
try {
fashiolista = new fashiolistaClass();
} catch(e) {
//we dont break other peoples code
}
}
},
BindReady method:
bindReady: function() {
if ( fashiolistaUtils.readyBound ) {
return;
}
fashiolistaUtils.readyBound = true;
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" ) {
return fashiolistaUtils.ready();
}
// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", FashiolistaDOMContentLoaded, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", fashiolistaUtils.ready, false );
// If IE event model is used
} else if ( document.attachEvent ) {
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent("onreadystatechange", FashiolistaDOMContentLoaded);
// A fallback to window.onload, that will always work
window.attachEvent( "onload", jQuery.ready );
// If IE and not a frame
// continually check to see if the document is ready
var toplevel = false;
try {
toplevel = window.frameElement == null;
} catch(e) {}
if ( document.documentElement.doScroll && toplevel ) {
doScrollCheck();
}
}
DomcontentLoaded and DoScrollCheck:
//
//Simulate an ondomload as well as we can without a framework
//Taken from jquery
//
// Cleanup functions for the document ready method
if ( document.addEventListener ) {
FashiolistaDOMContentLoaded = function() {
document.removeEventListener( "DOMContentLoaded", FashiolistaDOMContentLoaded, false );
fashiolistaUtils.ready();
};
} else if ( document.attachEvent ) {
FashiolistaDOMContentLoaded = function() {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if ( document.readyState === "complete" ) {
document.detachEvent( "onreadystatechange", FashiolistaDOMContentLoaded );
fashiolistaUtils.ready();
}
};
}
// The DOM ready check for Internet Explorer
function FashiolistaDoScrollCheck() {
if ( fashiolistaUtils.isReady ) {
return;
}
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
document.documentElement.doScroll("left");
} catch( error ) {
setTimeout( FashiolistaDoScrollCheck, 1 );
return;
}
// and execute any waiting functions
fashiolistaUtils.ready();
}
fashiolistaUtils.bindReady();