Instantly share code, notes, and snippets.

What would you like to do?
Loads different versions of YUI in sandboxed iframes for testing
Loads different versions of YUI in sandboxed iframes and makes them available
for use in the parent frame by exposing them as properties on a `Y.Multi`
The included `sandbox.html` file must be present at a URL with the same origin
as the current page, or the universe will explode.
This is primarily intended to be used for testing. You probably shouldn't use it
for anything real.
Load YUI 3.2.0 and make it available as `multi.YUI_320`. Load YUI 3.3.0 and
make it available as `multi.YUI_330`.
var multi = new Y.Multi({
sandbox: 'path/to/sandbox.html',
seeds: {
YUI_320: '',
YUI_330: ''
multi.on('ready', function () {
multi.YUI_320().use(function (Y) {
Y.log("OMG I'm using YUI 3.2.0!");
multi.YUI_330().use(function (Y) {
Y.log("OMG I'm using YUI 3.3.0!");
Load YUI 3.2.0 and 3.3.0, and instantiate both of them before firing the `ready`
event. The YUI instances, complete with `use()`ed modules attached, will be made
available on the `multi` instance.
var multi = new Y.Multi({
sandbox: 'path/to/sandbox.html',
seeds: {
Y_320: {
url: '',
use: ['node']
Y_330: {
url: '',
use: ['node']
multi.on('ready', function () {
multi.Y_320.log("OMG I'm using YUI 3.2.0!");
multi.Y_330.log("OMG I'm using YUI 3.3.0!");
@module multi
@class Multi
@param {Object} config Config object.
@param {Object} config.seeds Mapping of seed names to either URLs or config
@param {String} [config.sandbox='sandbox.html'] URL of the MultiYUI
`sandbox.html` file. Must be served from the same origin as the current
page, or the universe will explode.
YUI.add('multi', function (Y) {
var doc = Y.config.doc;
function Multi(config) {
this._config = Y.merge({
sandbox: 'sandbox.html',
seeds : {}
}, config);
this._iframes = [];
this._seedCount = Y.Object.size(this._config.seeds);
this._seedsLoaded = 0;
Fired when all requested seed files have been loaded (and instantiated, if
instantiation was requested). Subscribe to this event in order to be
notified when you can start using the requested YUI versions.
@event ready
this.publish('ready', {fireOnce: true});
Y.Object.each(this._config.seeds, this._createFrame, this);
Multi.prototype = {
_createFrame: function (seed, name) {
var iframe = doc.createElement('iframe'),
self = this,
url = seed.url || seed,
use = seed.use;
iframe.src = this._config.sandbox; = 'none';
iframe.onload = function () {
// Load the YUI seed URL into the iframe.
iframe.contentWindow.LazyLoad.js(url, function () {
// If a "use" array was passed, create a YUI instance before
// indicating completion. Otherwise, just attach the YUI global.
if (use) {
self[name] = iframe.contentWindow.YUI().use(use, function () {
} else {
self[name] = iframe.contentWindow.YUI;
doc.body.insertBefore(iframe, doc.body.firstChild);
_seedLoaded: function () {
this._seedsLoaded += 1;
if (this._seedsLoaded === this._seedCount) {'ready');
Y.augment(Multi, Y.EventTarget);
Y.Multi = Multi;
}, '3.3.0', {requires: ['event-custom']});
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
LazyLoad=function(j){function p(c,a){var g=j.createElement(c),b;for(b in a)a.hasOwnProperty(b)&&g.setAttribute(b,a[b]);return g}function m(c){var a=k[c],b,e;if(a)b=a.callback,e=a.urls,e.shift(),h=0,e.length||(b&&,a.obj),k[c]=null,n[c].length&&i(c))}function u(){if(!b){var c=navigator.userAgent;b={async:j.createElement("script").async===!0};(b.webkit=/AppleWebKit\//.test(c))||(||(b.opera=/Opera/.test(c))||(b.gecko=/Gecko\//.test(c))||(b.unknown=!0)}}function i(c,
a,g,e,h){var i=function(){m(c)},o=c==="css",f,l,d,q;u();if(a)if(a=typeof a==="string"?[a]:a.concat(),o||b.async||b.gecko||b.opera)n[c].push({urls:a,callback:g,obj:e,context:h});else{f=0;for(l=a.length;f<l;++f)n[c].push({urls:[a[f]],callback:f===l-1?g:null,obj:e,context:h})}if(!k[c]&&(q=k[c]=n[c].shift())){r||(r=j.head||j.getElementsByTagName("head")[0]);a=q.urls;f=0;for(l=a.length;f<l;++f)g=a[f],o?d=b.gecko?p("style"):p("link",{href:g,rel:"stylesheet"}):(d=p("script",{src:g}),d.async=!1),d.className=
"lazyload",d.setAttribute("charset","utf-8"),!o?d.onreadystatechange=function(){if(/loaded|complete/.test(d.readyState))d.onreadystatechange=null,i()}:o&&(b.gecko||b.webkit)?b.webkit?(q.urls[f]=d.href,s()):(d.innerHTML='@import "'+g+'";',m("css")):d.onload=d.onerror=i,r.appendChild(d)}}function s(){var c=k.css,a;if(c){for(a=t.length;--a>=0;)if(t[a].href===c.urls[0]){m("css");break}h+=1;c&&(h<200?setTimeout(s,50):m("css"))}}var b,r,k={},h=0,n={css:[],js:[]},t=j.styleSheets;return{css:function(c,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment