a.*******@gmail.com writes:
function abc(){
eval('function __newfunc(){alert("hello");}');
Which could just as well be:
function __newfunc(){alert("hello");}
}
abc();
now outside of that script context I try to call __newfunc(); and it
fails.
As it should.
In firefox and opera all I had to do is change eval to window.eval
to force it to execute in the window context. Unfortunetly that does
not work with IE.
It's not guaranteed to work like that, or at all. It's a proprietary
feature of those browsers that is not matched by other browsers. That
happens all the time, usually in the opposite direction :)
However I could just do the following: window.__newfunc =
__newfunc. and force it to register.
That's just assigning to a property of the global object, which makes
the assigned value available as a global variable.
My question is how can I do that without knowing the function name
of the one being defined inside the eval expression, in this case
__newfunc().
First of all, why do you want to use eval? Is the function declaration
provided by the user or in some other way not known when the abc
function was written?
Assuming the function comes as a string (bad choice, but it happens):
function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
}
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'