title: Is it possible to sandbox JavaScript running in the browser?
body: <p>I'm wondering if it's possible to sandbox JavaScript running in the browser to prevent access to features that are normally available to JavaScript code running in an HTML page.</p>
  <p>For example, let's say I want to provide a JavaScript API for end users to let them define event handlers to be run when &quot;interesting events&quot; happen, but I don't want those users to access the properties and functions of the <code>window</code> object. Am I able to do this?</p>
  <p>In the simplest case, let's say I want to prevent users calling <code>alert</code>. A couple of approaches I can think of are:</p>
  <ul>
  <li>Redefine <code>window.alert</code> globally. I don't think this would be a valid approach because other code running in the page (i.e., stuff not authored by users in their event handlers) might want to use <code>alert</code>.</li>
  <li>Send the event handler code to the server to process. I'm not sure that sending the code to the server to process is the right approach, because the event handlers need to run in the context of the page.</li>
  </ul>
  <p>Perhaps a solution where the server processes the user defined function and then generates a callback to be executed on the client would work? Even if that approach works, are there better ways to solve this problem?</p>