mozilla

Revision 101215 of Components.utils.evalInSandbox

  • Revision slug: Components.utils.evalInSandbox
  • Revision title: Components.utils.evalInSandbox
  • Revision id: 101215
  • Created:
  • Creator: Ted_Mielczarek
  • Is current revision? No
  • Comment Creating minimal page

Revision Content

Introduction

In certain circumstances, you may want to evaluate JavaScript code with restricted privileges. In Firefox 1.5 (Gecko 1.8) or later, an API exists to allow you to do this. It contains the notion of a "sandbox" that you can create and evaluate code in its context.

Use

To use evalInSandbox, you must first create a sandbox object using its constructor, Components.utils.Sandbox. The sandbox must be initialized with a principal URI.

// create a sandbox with a given principal
var s = Components.utils.Sandbox("http://www.example.com/");
// the sandbox object is the global scope object
// for script you execute
s.y = 5;
var result = Components.utils.evalInSandbox("x = y + 2; x + 3", s);
// result is 10, s.x is now 7

Revision Source

<h3 name="Introduction"> Introduction </h3>
<p>In certain circumstances, you may want to evaluate <a href="en/JavaScript">JavaScript</a> code with restricted privileges.  In <a href="en/Firefox_1.5">Firefox 1.5</a> (Gecko 1.8) or later, an API exists to allow you to do this.  It contains the notion of a "sandbox" that you can create and evaluate code in its context.
</p>
<h4 name="Use"> Use </h4>
<p>To use evalInSandbox, you must first create a sandbox object using its constructor, <code>Components.utils.Sandbox</code>.  The sandbox must be initialized with a principal URI.
</p>
<pre>// create a sandbox with a given principal
var s = Components.utils.Sandbox("http://www.example.com/");
// the sandbox object is the global scope object
// for script you execute
s.y = 5;
var result = Components.utils.evalInSandbox("x = y + 2; x + 3", s);
// result is 10, s.x is now 7
</pre>
Revert to this revision