How to call global function having its name?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • szimek

    How to call global function having its name?

    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
  • Matt Kruse

    #2
    Re: How to call global function having its name?

    On Feb 13, 9:23 am, szimek <szi...@gmail.c omwrote:
    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

    Comment

    • SAM

      #3
      Re: How to call global function having its name?

      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 ?

      --
      sm

      Comment

      Working...