Browse files

2 renditions added for attachDocumentReadyListener

  • Loading branch information...
1 parent ed98128 commit 8ff31cb7a4b390b1fa33aad4e339e3a7813ec444 @adamsilver adamsilver committed Mar 1, 2012
View
20 functions/attachDocumentReadyListener/rendition1.js
@@ -0,0 +1,20 @@
+// Degrades in IE 8-
+// No frames or other alternate windows
+// Best used with asset-light documents
+
+var attachDocumentReadyListener
+ readyListenerAttached;
+
+if(isHostMethod(global, "addEventListener")) {
+ attachDocumentReadyListener = function(fn) {
+
+ // Remove this "scaffolding" on deployment
+ if(readyListenerAttached) {
+ throw new Error("One too many ready listeners. Use a queue!");
+ }
+ readyListenerAttached = true;
+
+ // Production function starts (and ends) here
+ window.addEventListener('load', fn, false);
+ };
+};
View
32 functions/attachDocumentReadyListener/rendition2.js
@@ -0,0 +1,32 @@
+// Degrades in IE 8-
+// No frames or other alternate windows
+
+var attachDocumentReadyListener,
+ readyListener,
+ readyListenerAttached,
+ documentIsReady;
+
+if (isHostMethod(global, "addEventListener") && isHostMethod(globalDocument, "addEventListener")) {
+ attachDocumentReadyListener = function(fn) {
+
+ // Remove this "scaffolding" on deployment
+ if (readyListenerAttached) {
+ throw new Error('One too many ready listeners. Use a queue!');
+ }
+
+ readyListenerAttached = true;
+
+ // Production function starts here
+
+ // Run on first fired event
+ readyListener = function() {
+ if (!documentReady) {
+ documentReady = true;
+ fn();
+ }
+ };
+
+ global.addEventListener('load', readyListener, false);
+ globalDocument.addEventListener('DOMContentLoaded', readyListener, false);
+ };
+};

0 comments on commit 8ff31cb

Please sign in to comment.