$.globalEval in native javascript
29 July 2013
eval
allows developers to execute code and create variables in the scope in which it is executed.
For Example:
function evalExec(){ eval('var a=10;'); } evalExec();
When we execute evalExec
function a
variable is created inside evalExec function's scope.
What do we do when we want the code to be evaluated in the global scope?
jQuery provides a $.globalEval function which executes the code in global scope. If we want to achieve same thing via javascript, we can do it like this -
function globalEvalExec(){ window.eval.call(window,'var a=10;'); } globalEvalExec();
This creates a variable a
in the global scope and it is accessible via window.a
More Information
blog comments powered by Disqus