Skip to content

Instantly share code, notes, and snippets.

@op1ekun
Created June 20, 2013 23:59
Show Gist options
  • Save op1ekun/5827838 to your computer and use it in GitHub Desktop.
Save op1ekun/5827838 to your computer and use it in GitHub Desktop.
jQuery as a custom YUI module
// 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