The Ashes

Technology, Science and other news
June 27, 2009

JavaScript sandbox using Web Workers

Posted by : admin
Filed under : General

We have been sandboxing JavaScript in iframes for a long time. The Web Worker API has the nice property that it doesn’t have access to objects like document and the like, and just runs code that you can pass over to it.

With this, Elijah Grey has created an experimental jsandbox API that gives you an eval function that you pass in some code, and optionally input data, callback for results, and an onerror callback.

Code looks like this:

PLAIN TEXT
JAVASCRIPT:

  1.  
  2. jsandbox
  3.     .eval({
  4.       code    : “x=1;Math.round(Math.pow(input, ++x))”,
  5.       input   : 36.565010597564445,
  6.       callback: function(n) {
  7.           console.log(“number: “, n); // number: 1337
  8.       }
  9.   }).eval({
  10.       code   : “][];.]\\ (*# ($(! ~”,
  11.       onerror: function(ex) {
  12.           console.log(“syntax error: “, ex); // syntax error: [error object]
  13.       }
  14.   }).eval({
  15.       code    : ‘"foo"+input’,
  16.       input   : “bar”,
  17.       callback: function(str) {
  18.           console.log(“string: “, str); // string: foobar
  19.       }
  20.   }).eval({
  21.       code    : “({q:1, w:2})”,
  22.       callback: function(obj) {
  23.           console.log(“object: “, obj); // object: object q=1 w=2
  24.       }
  25.   }).eval({
  26.       code    : “[1, 2, 3].concat(input)”,
  27.       input   : [4, 5, 6],
  28.       callback: function(arr) {
  29.           console.log(“array: “, arr); // array: [1, 2, 3, 4, 5, 6]
  30.       }
  31.   }).eval({
  32.       code    : “function x(z){this.y=z;};new x(input)”,
  33.       input   : 4,
  34.       callback: function(x) {
  35.           console.log(“new x: “, x); // new x: object y=4
  36.       }
  37.   });
  38.  
Tags :

No Comments

(required)
(will not be published) (required)
(opitional)

cciash.com EN ES IT DE PT CZ FR RU
February 2018
M T W T F S S
« Sep    
 1234
567891011
12131415161718
19202122232425
262728  

Pages

Categories

Resources

There are many online poker site where you can play but at poker.hk you can play the poker games with all the knowledge you need related to the game with the poker school available in both the English and Chinese language.

Super Casino

Now you can bet on any sports and any sporting events from all the comfort from your home. Bet770 allows you to bet on any events and match with in just 3 clicks. They also offers great odds on football betting for every premier and champions league match. Get £50 free in bets when you register.

Bingo770, offering best online bingo games with £7.70 free no deposit Bonus!