JavaScript: conditionally invoking functions within a function

Discussion in 'HTML' started by Spartanicus, Sep 30, 2005.

  1. Spartanicus

    Spartanicus Guest

    How should functions be invoked conditionally from within a function?
    This throws errors and doesn't work:

    function test() {
    if (0 == 0)
    {
    function foo();
    }
    if (1 == 1)
    {
    function bar();
    }
    }

    Minimized test case: http://homepage.ntlworld.com/spartanicus/temp.htm

    --
    Spartanicus
     
    Spartanicus, Sep 30, 2005
    #1
    1. Advertising

  2. Spartanicus

    Oli Filth Guest

    Spartanicus said the following on 30/09/2005 14:15:
    > How should functions be invoked conditionally from within a function?
    > This throws errors and doesn't work:
    >
    > function test() {
    > if (0 == 0)
    > {
    > function foo();

    ^
    ^
    You don't need "function" here.
    > }
    > if (1 == 1)
    > {
    > function bar();

    ^
    ^
    Nor here.
    > }
    > }
    >



    --
    Oli
     
    Oli Filth, Sep 30, 2005
    #2
    1. Advertising

  3. Spartanicus

    Neredbojias Guest

    With neither quill nor qualm, Spartanicus quothed:

    > How should functions be invoked conditionally from within a function?
    > This throws errors and doesn't work:
    >
    > function test() {
    > if (0 == 0)
    > {
    > function foo();
    > }
    > if (1 == 1)
    > {
    > function bar();
    > }
    > }
    >
    > Minimized test case: http://homepage.ntlworld.com/spartanicus/temp.htm


    You've basically got it right.

    function test() {
    if (a) {
    foo();
    } else {
    if (b) bar();
    }
    }

    --
    Neredbojias
    Contrary to popular belief, it is believable.
     
    Neredbojias, Sep 30, 2005
    #3
  4. Spartanicus

    Spartanicus Guest

    Oli Filth <> wrote:

    >> How should functions be invoked conditionally from within a function?
    >> This throws errors and doesn't work:


    >You don't need "function" here.


    That's the second time that I coded an error in the minimized example
    that wasn't in the actual code. Fortunately I've now pinned down the
    cause of the trouble with the actual script, thanks for your help.

    --
    Spartanicus
     
    Spartanicus, Sep 30, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cgia
    Replies:
    1
    Views:
    476
    Matt Berther
    Apr 20, 2004
  2. Mike
    Replies:
    1
    Views:
    666
  3. Replies:
    6
    Views:
    344
    William Manegold
    Jan 19, 2006
  4. kaens
    Replies:
    1
    Views:
    248
    Dan Bishop
    May 26, 2007
  5. Seth Williams

    conditionally run Javascript alert

    Seth Williams, Mar 18, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    490
    Teemu Keiski
    Mar 19, 2008
Loading...

Share This Page