/ Published in: JavaScript
URL: http://www.freelancephp.net/domready-javascript-object-cross-browser/
Small object for cross-browser compatibility of the DOMContentLoaded event. Easily add functions that will be executed when the DOM is ready.
Expand |
Embed | Plain Text
var DOMReady = (function () { var fns = [], isReady = false, ready = function () { isReady = true; // call all functions for ( var x = 0; x < fns.length; x++ ) fns[x](); }; // public add method this.add = function ( fn ) { // eval string in a function if ( fn.constructor == String ) { var strFunc = fn; fn = function () { eval( strFunc ); }; } // call imediately when DOM is already ready if ( isReady ) { fn(); } else { // add to the list fns[fns.length] = fn; } }; // For all browsers except IE if ( window.addEventListener ) document.addEventListener( 'DOMContentLoaded', function(){ ready(); }, false ); // For IE // Code taken from http://ajaxian.com/archives/iecontentloaded-yet-another-domcontentloaded (function(){ // check IE's proprietary DOM members if ( ! document.uniqueID && document.expando ) return; // you can create any tagName, even customTag like <document :ready /> var tempNode = document.createElement('document:ready'); try { // see if it throws errors until after ondocumentready tempNode.doScroll('left'); // call ready ready(); } catch ( err ) { setTimeout(arguments.callee, 0); } })(); return this; })();
You need to login to post a comment.
