Created
June 20, 2013 23:59
-
-
Save op1ekun/5827838 to your computer and use it in GitHub Desktop.
jQuery as a custom YUI module
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
// version with iframe | |
YUI.add('dlp-jquery', function(Y) { | |
// create iframe dynamically to create a sandbox for our script | |
var myIframe = document.createElement('iframe'); | |
myIframe.id = 'jQueryIframe'; | |
// callback run when the iframe is loaded | |
myIframe.onload = function() { | |
console.log('my iframe loaded'); | |
var jqscript = myIframe.contentDocument.createElement('script'); | |
jqscript.src = 'http://code.jquery.com/jquery-1.10.1.min.js'; | |
// callback run when the script is loaded | |
function onLoadCb() { | |
console.log('script loaded'); | |
// add jQuery to dlp namespace | |
Y.namespace('dlp') | |
.jQuery = myIframe.contentWindow.jQuery; | |
// cleanup | |
myIframe.parentNode.removeChild(myIframe); | |
} | |
// feature detection | |
if (jqscript.onload === null) { | |
jqscript.onload = onLoadCb; | |
} | |
else { | |
jqscript.onreadystatechange = onLoadCb; | |
} | |
// append script, it will trigger onload/onreadystatechange when successful | |
myIframe.contentDocument.body.appendChild(jqscript); | |
} | |
// append iframe, it will trigger onload when successful | |
document.body.appendChild(myIframe); | |
}, '1.10.1'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment