2

I have two JavaScript function. I want to call a function by another function argument. Like this code:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv='Content-type' content='text/html; charset=utf-8'>
    <title>Javascript Function</title>
    <script language="javascript" type="text/javascript">
        function fOne()
        {
            alert("Function One");
        }
        function fTwo(f_name)
        {
            f_name; // I want to Call fOne() here
        }
    </script>
</head>
<body>
    <a href="#" onclick="fTwo('fOne')">Call Function</a>
</body>
</html>

Is this posible in any way?

Mobo
  • 118
  • 1
  • 1
  • 7

5 Answers5

4
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv='Content-type' content='text/html; charset=utf-8'>
    <title>Javascript Function</title>
    <script language="javascript" type="text/javascript">
        function fOne()
        {
            alert("Function One");
        }
        function fTwo(f_name)
        {
            f_name(); // I want to Call fOne() here
        }
    </script>
</head>
<body>
    <a href="#" onclick="fTwo(fOne)">Call Function</a>
</body>
</html>

This is all that you need

Akshay Khandelwal
  • 1,570
  • 11
  • 19
  • 1
    Additionally, check out the "call" and "apply" methods -- they let you set the "this" object, and "apply" lets you pass an array to represent all the arguments (for when you don't know at code-time how many arguments there will be). – Max Oct 12 '13 at 15:36
3

You can do this:

function one() {
    alert('one');
}

function two(fname) {
    window[fname]();
}

two('one');

For a more complete answer, see here: How to execute a JavaScript function when I have its name as a string

Community
  • 1
  • 1
Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59
1

Try like this:-

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv='Content-type' content='text/html; charset=utf-8'>
    <title>Javascript Function</title>
    <script language="javascript" type="text/javascript">
        function fOne()
        {
            alert("Function One");
        }
        function fTwo(f_name)
        {
            f_name(); // I want to Call fOne() here
        }
    </script>
</head>
<body>
    <a href="#" onclick="fTwo(fOne)">Call Function</a>
</body>
</html>
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

The best way to do this is to pass a reference to your function (fOne) and not a string (other answers are already covering this). However, if you really want to call your function by its name, you can look it up in the window object:

function fOne(){ console.log("fOne"); }
function fCall (f_name) { 
    window[f_name]();
}

fCall("fOne"); // results in console.log.

However, stringly code tends to be more error prone, so use the method provided by Akshay or Rahul instead.

Zeta
  • 103,620
  • 13
  • 194
  • 236
-1
function fTwo(f_name)
{
    eval(f_name+"()");
}

I'll let someone cleverer than I explain the security implications.

Hans Z
  • 4,664
  • 2
  • 27
  • 50