Created
January 28, 2012 12:30
-
-
Save ryandesign/1694157 to your computer and use it in GitHub Desktop.
Modify contentloaded.js callback to allow specifying what to use for "this"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- contentloaded.js.orig | |
+++ contentloaded.js | |
@@ -15,12 +15,14 @@ | |
// @win window reference | |
// @fn function reference | |
-function contentLoaded(win, fn) { | |
+function contentLoaded(win, fn, thisp) { | |
var done = false, top = true, | |
doc = win.document, root = doc.documentElement, | |
+ thisp = thisp ? thisp : win, | |
+ | |
add = doc.addEventListener ? 'addEventListener' : 'attachEvent', | |
rem = doc.addEventListener ? 'removeEventListener' : 'detachEvent', | |
pre = doc.addEventListener ? '' : 'on', | |
@@ -28,7 +30,7 @@ | |
init = function(e) { | |
if (e.type == 'readystatechange' && doc.readyState != 'complete') return; | |
(e.type == 'load' ? win : doc)[rem](pre + e.type, init, false); | |
- if (!done && (done = true)) fn.call(win, e.type || e); | |
+ if (!done && (done = true)) fn.call(thisp, e.type || e); | |
}, | |
poll = function() { | |
@@ -36,7 +38,7 @@ | |
init('poll'); | |
}; | |
- if (doc.readyState == 'complete') fn.call(win, 'lazy'); | |
+ if (doc.readyState == 'complete') fn.call(thisp, 'lazy'); | |
else { | |
if (doc.createEventObject && root.doScroll) { | |
try { top = !win.frameElement; } catch(e) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="src/ContentLoaded/contentloaded.js"></script> | |
<script type="text/javascript"> | |
function Foo(id) { | |
this.id = id; | |
contentLoaded(window, function() { | |
this.element = document.getElementById(this.id); | |
this.element.innerHTML = 'loaded ' + this.id; | |
}, this); | |
} | |
var foo1 = new Foo('div1'); | |
var foo2 = new Foo('div2'); | |
</script> | |
</head> | |
<body> | |
<div id="div1">loading div1</div> | |
<div id="div2">loading div2</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment