Ryan Grove![]()
Sandbox simplifies the process of creating isolated iframe sandboxes in which to evaluate JavaScript code for tasks like profiling or unit testing.
Note that these sandboxes, while isolated enough to be used for testing, are not secure. They should be used only to run trusted code, and are not intended to be used to execute arbitrary untrusted JavaScript.
<script src="http://yui.yahooapis.com/3.1.1/build/yui/yui-min.js"></script>YUI({
//Last Gallery Build of this module
gallery: 'gallery-2010.05.12-19-08'
}).use('gallery-sandbox', function(Y) {
var sandbox = new Y.Sandbox();
// Listen for the 'ready' event to ensure that the sandbox iframe has
// been initialized before you try to use it.
sandbox.on('ready', function () {
// Run some code in the sandbox.
sandbox.run('alert("Hello from " + document.title);');
// You can also pass in a function, although it won't retain its
// execution context when it runs in the sandbox since it will be
// cast to a string and then evaled.
sandbox.run(function () { alert("I like pie."); });
// If your code will perform async operations and you'd like to be
// notified when they've finished, call done() from the sandbox code
// and pass in a callback to receive the finish notification.
sandbox.run(function () {
// Runs in sandbox.
setTimeout(function () {
done();
}, 1000);
}, function () {
// Runs in parent after the sandbox code calls done().
alert('All done!');
});
// For easy profiling, run code with profile(). Call done() from the
// sandbox code to indicate completion. Provide a callback to
// receive the profile data once it's ready.
sandbox.profile(function () {
var i = 5000000;
while (i--) {
Math.sqrt(Math.PI);
}
done();
}, function (profileData) {
alert("That took " + profileData.duration + " milliseconds.");
});
// Pass data between the parent and the sandbox using a shared
// environment object.
sandbox.setEnvValue('kittens', 'fuzzy');
sandbox.run(function () {
alert('Kittens are ' + sandbox.kittens);
sandbox.ninjas = 'sneaky';
});
alert('Ninjas are ' + sandbox.getEnvValue('ninjas'));
// Clean up the sandbox.
sandbox.destroy();
// See the API docs for even more useful stuff.
});
});No forum posts for this module.
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info