I am writing php framework that extracts Javascript blocks to merge, minify and execute them asynchronously.
So i like to delay Javascript code execution, to do that i wrap the javascript in
MyLib.then(function(){
//orignial code from the templates....
}
this works fine unless the wrapped code needs to create global variables (i am not the author of the wrapped code, so i do not want to fix that code) instead i like to run that code in a global execution context.
for example:
//excample code not within my controle
var global="Hello World";
would become:
MyLib.then(function(){
//excample code not within my controle
var global="Hello World";
}
the solution right now is to put the javascript into an string and then
MyLib.then(function(){
$.globalEval("var global=\"Hello World\"";");
}
or with the globalEval call within my library
MyLib.then("var global=\"Hello World\"";")
but i would like to stick to the better readable version, which is also easier to debug for users of the framework.
MyLib.then(function(){
var global="Hello World";
}
and change the execution context within my lib by using callback.call(window) or something like that, is that possible?