diff --git a/src/lib/ondomready.js b/src/lib/ondomready.js index ad85bf0..4ad5bbe 100644 --- a/src/lib/ondomready.js +++ b/src/lib/ondomready.js @@ -8,146 +8,146 @@ var win = window; //Lazy loading fix for Firefox < 3.6 //http://webreflection.blogspot.com/2009/11/195-chars-to-help-lazy-loading.html - if (document.readyState == null && document.addEventListener) { - document.addEventListener("DOMContentLoaded", function DOMContentLoaded() { - document.removeEventListener("DOMContentLoaded", DOMContentLoaded, false); - document.readyState = "complete"; - }, false); - document.readyState = "loading"; - } +if (document.readyState == null && document.addEventListener) { + document.addEventListener("DOMContentLoaded", function DOMContentLoaded() { + document.removeEventListener("DOMContentLoaded", DOMContentLoaded, false); + document.readyState = "complete"; + }, false); + document.readyState = "loading"; +} - var doc = win.document, - docElem = doc.documentElement, +var doc = win.document, + docElem = doc.documentElement, - LOAD = "load", - FALSE = false, - ONLOAD = "on"+LOAD, - COMPLETE = "complete", - READYSTATE = "readyState", - ATTACHEVENT = "attachEvent", - DETACHEVENT = "detachEvent", - ADDEVENTLISTENER = "addEventListener", - DOMCONTENTLOADED = "DOMContentLoaded", - ONREADYSTATECHANGE = "onreadystatechange", - REMOVEEVENTLISTENER = "removeEventListener", + LOAD = "load", + FALSE = false, + ONLOAD = "on"+LOAD, + COMPLETE = "complete", + READYSTATE = "readyState", + ATTACHEVENT = "attachEvent", + DETACHEVENT = "detachEvent", + ADDEVENTLISTENER = "addEventListener", + DOMCONTENTLOADED = "DOMContentLoaded", + ONREADYSTATECHANGE = "onreadystatechange", + REMOVEEVENTLISTENER = "removeEventListener", - // W3C Event model - w3c = ADDEVENTLISTENER in doc, - top = FALSE, + // W3C Event model + w3c = ADDEVENTLISTENER in doc, + _top = FALSE, - // isReady: Is the DOM ready to be used? Set to true once it occurs. - isReady = FALSE, + // isReady: Is the DOM ready to be used? Set to true once it occurs. + isReady = FALSE, - // Callbacks pending execution until DOM is ready - callbacks = []; + // Callbacks pending execution until DOM is ready + callbacks = []; - // Handle when the DOM is ready - function ready( fn ) { - if ( !isReady ) { +// Handle when the DOM is ready +function ready( fn ) { + if ( !isReady ) { - // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). - if ( !doc.body ) { - return defer( ready ); - } + // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). + if ( !doc.body ) { + return defer( ready ); + } - // Remember that the DOM is ready - isReady = true; + // Remember that the DOM is ready + isReady = true; - // Execute all callbacks - while ( fn = callbacks.shift() ) { - defer( fn ); - } + // Execute all callbacks + while ( fn = callbacks.shift() ) { + defer( fn ); } } +} - // The ready event handler - function completed( event ) { - // readyState === "complete" is good enough for us to call the dom ready in oldIE - if ( w3c || event.type === LOAD || doc[READYSTATE] === COMPLETE ) { - detach(); - ready(); - } +// The ready event handler +function completed( event ) { + // readyState === "complete" is good enough for us to call the dom ready in oldIE + if ( w3c || event.type === LOAD || doc[READYSTATE] === COMPLETE ) { + detach(); + ready(); } +} - // Clean-up method for dom ready events - function detach() { - if ( w3c ) { - doc[REMOVEEVENTLISTENER]( DOMCONTENTLOADED, completed, FALSE ); - win[REMOVEEVENTLISTENER]( LOAD, completed, FALSE ); - } else { - doc[DETACHEVENT]( ONREADYSTATECHANGE, completed ); - win[DETACHEVENT]( ONLOAD, completed ); - } - } - - // Defers a function, scheduling it to run after the current call stack has cleared. - function defer( fn, wait ) { - // Allow 0 to be passed - setTimeout( fn, +wait >= 0 ? wait : 1 ); - } - - // Attach the listeners: - - // Catch cases where onDomReady is called after the browser event has already occurred. - // we once tried to use readyState "interactive" here, but it caused issues like the one - // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15 - if ( doc[READYSTATE] === COMPLETE ) { - // Handle it asynchronously to allow scripts the opportunity to delay ready - defer( ready ); - - // Standards-based browsers support DOMContentLoaded - } else if ( w3c ) { - // Use the handy event callback - doc[ADDEVENTLISTENER]( DOMCONTENTLOADED, completed, FALSE ); - - // A fallback to window.onload, that will always work - win[ADDEVENTLISTENER]( LOAD, completed, FALSE ); - - // If IE event model is used +// Clean-up method for dom ready events +function detach() { + if ( w3c ) { + doc[REMOVEEVENTLISTENER]( DOMCONTENTLOADED, completed, FALSE ); + win[REMOVEEVENTLISTENER]( LOAD, completed, FALSE ); } else { - // Ensure firing before onload, maybe late but safe also for iframes - doc[ATTACHEVENT]( ONREADYSTATECHANGE, completed ); + doc[DETACHEVENT]( ONREADYSTATECHANGE, completed ); + win[DETACHEVENT]( ONLOAD, completed ); + } +} - // A fallback to window.onload, that will always work - win[ATTACHEVENT]( ONLOAD, completed ); +// Defers a function, scheduling it to run after the current call stack has cleared. +function defer( fn, wait ) { + // Allow 0 to be passed + setTimeout( fn, +wait >= 0 ? wait : 1 ); +} - // If IE and not a frame - // continually check to see if the document is ready - try { - top = win.frameElement == null && docElem; - } catch(e) {} +// Attach the listeners: - if ( top && top.doScroll ) { - (function doScrollCheck() { - if ( !isReady ) { - try { - // Use the trick by Diego Perini - // http://javascript.nwbox.com/IEContentLoaded/ - top.doScroll("left"); - } catch(e) { - return defer( doScrollCheck, 50 ); - } +// Catch cases where onDomReady is called after the browser event has already occurred. +// we once tried to use readyState "interactive" here, but it caused issues like the one +// discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15 +if ( doc[READYSTATE] === COMPLETE ) { + // Handle it asynchronously to allow scripts the opportunity to delay ready + defer( ready ); - // detach all dom ready events - detach(); +// Standards-based browsers support DOMContentLoaded +} else if ( w3c ) { + // Use the handy event callback + doc[ADDEVENTLISTENER]( DOMCONTENTLOADED, completed, FALSE ); - // and execute any waiting functions - ready(); + // A fallback to window.onload, that will always work + win[ADDEVENTLISTENER]( LOAD, completed, FALSE ); + +// If IE event model is used +} else { + // Ensure firing before onload, maybe late but safe also for iframes + doc[ATTACHEVENT]( ONREADYSTATECHANGE, completed ); + + // A fallback to window.onload, that will always work + win[ATTACHEVENT]( ONLOAD, completed ); + + // If IE and not a frame + // continually check to see if the document is ready + try { + _top = win.frameElement == null && docElem; + } catch(e) {} + + if ( _top && _top.doScroll ) { + (function doScrollCheck() { + if ( !isReady ) { + try { + // Use the trick by Diego Perini + // http://javascript.nwbox.com/IEContentLoaded/ + _top.doScroll("left"); + } catch(e) { + return defer( doScrollCheck, 50 ); } - })(); - } - } - function onDomReady( fn ) { - // If DOM is ready, execute the function (async), otherwise wait - isReady ? defer( fn ) : callbacks.push( fn ); - } + // detach all dom ready events + detach(); - // Add version - onDomReady.version = "1.4.0"; - // Add method to check if DOM is ready - onDomReady.isReady = function(){ - return isReady; - }; + // and execute any waiting functions + ready(); + } + })(); + } +} + +function onDomReady( fn ) { + // If DOM is ready, execute the function (async), otherwise wait + isReady ? defer( fn ) : callbacks.push( fn ); +} + +// Add version +onDomReady.version = "1.4.0"; +// Add method to check if DOM is ready +onDomReady.isReady = function(){ + return isReady; +}; module.exports = onDomReady; \ No newline at end of file diff --git a/src/lib/polyfills.js b/src/lib/polyfills.js index 330b469..67d59ea 100644 --- a/src/lib/polyfills.js +++ b/src/lib/polyfills.js @@ -51,7 +51,7 @@ if (!Object.keys) { //https://github.com/inexorabletash/polyfill/blob/master/web.js (function (global) { - var B64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + var B64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; global.atob = global.atob || function (input) { input = String(input); var position = 0, @@ -60,8 +60,8 @@ if (!Object.keys) { input = input.replace(/\s/g, ''); if ((input.length % 4) === 0) { input = input.replace(/=+$/, ''); } - if ((input.length % 4) === 1) { throw Error("InvalidCharacterError"); } - if (/[^+/0-9A-Za-z]/.test(input)) { throw Error("InvalidCharacterError"); } + if ((input.length % 4) === 1) { throw Error('InvalidCharacterError'); } + if (/[^+/0-9A-Za-z]/.test(input)) { throw Error('InvalidCharacterError'); } while (position < input.length) { n = B64_ALPHABET.indexOf(input.charAt(position)); @@ -97,7 +97,7 @@ if (!Object.keys) { o1, o2, o3, e1, e2, e3, e4; - if (/[^\x00-\xFF]/.test(input)) { throw Error("InvalidCharacterError"); } + if (/[^\x00-\xFF]/.test(input)) { throw Error('InvalidCharacterError'); } while (position < input.length) { o1 = input.charCodeAt(position++); @@ -133,7 +133,7 @@ if (!Object.prototype.hasOwnProperty){ Object.prototype.hasOwnProperty = function(prop) { var proto = this.__proto__ || this.constructor.prototype; return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]); - } + }; /*jshint +W001, +W103 */ } @@ -169,7 +169,7 @@ if (!window.requestAnimationFrame) { (function (global) { global.requestAnimationFrame = function (callback) { return global.setTimeout(callback, 1000 / 60); - } + }; global.cancelAnimationFrame = global.clearTimeout; })(this);