How to call global function having its name?

S

szimek

Hi!

In an app which I'm trying to fix to work with FF there's a lot of
code like eval("function_name();"). I remember that there's some way
to call global functions using window object, but I can't get it to
work. Here's what I tried (in Firebug console):

function a() {alert("a called")};
a(); // works
eval("a();"); // works
window["a"](); // doesn't work
TypeError: window.a is not a function

window.b = function() {alert("b called");}
window["b"](); // works

So how can I call global functions in other way than using eval?

Thanks in advance
 
M

Matt Kruse

In an app which I'm trying to fix to work with FF there's a lot of
code like eval("function_name();"). I remember that there's some way
to call global functions using window object, but I can't get it to
work. Here's what I tried (in Firebug console):

Running this code in the firebug console is not the same as running it
in the browser window.

Try creating a local file with your code and opening it in FF and
you'll see that it works fine.

Matt Kruse
 
S

SAM

szimek a écrit :
Hi!

In an app which I'm trying to fix to work with FF there's a lot of
code like eval("function_name();"). I remember that there's some way
to call global functions using window object, but I can't get it to
work. Here's what I tried (in Firebug console):

function a() {alert("a called")};
a(); // works
eval("a();"); // works
window["a"](); // doesn't work

?? that works with my Firefox


function b() { alert('yes ? who calls me ?'); };
window['b']();
So how can I call global functions in other way than using eval?

/* this bellow also works for me */

var a = function() { alert('yes ?'); };

window['a'](); // works



to call a function (global) :

<a href="#" onclick="a(); return false;">test</a>

Why to want more ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top