Robust global eval

Robust implementation of an eval method that runs its code in the global scope, should work in nearly any enviroment.

by Entoarox

HTML

Robust support for a global code evaluation function. It should work in nearly any situation, even in non-browser enviroments.<br />
Note that unlike normal <code>eval</code> calls, the <code>globalEval</code> function does not return a result, if you need a result make sure to set a global variable in the evaluated code that you can check afterwards.

JavaScript

// The globalEval method, made by Entoarox using research of kangax 
var globalEval=(function(d){var i=(function(o,Object){try{return (1,eval)('Object')===o}catch(e){return false}})(Object,123);if(i){return function(c){(1,eval)(c)}}else if(typeof execScript==='function'){return function(c){execScript(c)}}else if(d.createElement && d.createTextNode){return function(c){var n=d.createElement('script');n.appendChild(d.createTextNode(c));d.body.appendChild(n)}}else if(typeof setTimeout==='function'){return function(c){setTimeout(c)}}})(document);
// Test case, checks if the enviroment used allows globalEval to work properly
globalEvalWorks=true;// Not ES5-strict, but needed to dodge the chain
(function(){
    var globalEvalWorks=false;
    globalEval('globalEvalWorks?alert("The globalEval() method runs in the global scope."):alert("The globalEval() method does NOT run in the global scope.")')
})()