on-internet
← Community

Javascript eval on global scope?

Stack Overflow ·

Is it possible to use the eval command to execute something with a global scope? For example, this will cause an error: <script> function execute(x){ eval(x); } function start(){ execute("var ary = new Array()"); execute("ary.push('test');"); // This will cause exception: ary is not defined } </script> <html><body onLoad="start()"></body></html> I know the 'with' keyword will set a specific scope, but is there a keyword for the global scope? Or is it possible to define a custom scope that would allow this to work? <script> var scope = {}; function execute(x){ with(scope){ eval(x); } } function

Comments

Sign in or sign up to comment and upvote.

No comments yet.