469,366 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

javascript dereference?

Hi,

I have the following code:

function abc(){
eval('function __newfunc(){alert("hello");}');
}
abc();

now outside of that script context I try to call __newfunc(); and it
fails.
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.
However
I could just do the following: window.__newfunc = __newfunc. and force
it to register.
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().

Thanks

Aug 5 '06 #1
5 5064
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.'
Aug 5 '06 #2


Lasse Reichstein Nielsen wrote:

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor does
the script engine used in Opera 8 or 9.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 5 '06 #3
Lasse Reichstein Nielsen wrote:
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;
}
Unfortunetly none of the two is the case. Infact I dont know what the
javascript code would be. It may define from 1-n number of functions.
Also I cant know before hand what the function name would be.
I am trying to find a way to execute the javascript code without having
to do something like:
document.write('<script language="javascript"code snip here
</script>');

/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.'
Aug 5 '06 #4
Martin Honnen <ma*******@yahoo.dewrites:
Lasse Reichstein Nielsen wrote:

> function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor
does the script engine used in Opera 8 or 9.
True. And I thought I checked in Opera, but it must have been Firefox.

Well, in that case, one will have to pick the name out of the string:

function abc(funcString) {
var f = eval("("+funcString+")");
var nameMatch = /function\s+([\w$]+)\s*\(/.exec(funcString);
if (nameMatch) {
window[nameMatch[1]] = 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.'
Aug 5 '06 #5
JRS: In article <44***********************@newsread4.arcor-online.net>,
dated Sat, 5 Aug 2006 14:02:14 remote, seen in
news:comp.lang.javascript, Martin Honnen <ma*******@yahoo.deposted :
>

Lasse Reichstein Nielsen wrote:

> function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor does
the script engine used in Opera 8 or 9.
Let the function be F : will the second word in F.toString() always be
F, the 'name' of the function?
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 6 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Matthias Kaeppler | last post: by
1 post views Thread by jarit | last post: by
5 posts views Thread by Kurt Lange | last post: by
6 posts views Thread by TuxC0d3 | last post: by
30 posts views Thread by somenath | last post: by
17 posts views Thread by Tony Jackson | last post: by
26 posts views Thread by =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.