Results 1 to 2 of 2

Thread: Calling A Function

  1. #1
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Calling A Function

    I would like to call a function within a function. Is this even possible??
    When I do this I get "undefined"

    Here's my code:
    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    function product(a,b){
       total = a*b;
       display(total);
    }
    
    function display(num){
       return num;
    }
    </script>
    </head>
    
    <body>
    <h1>This text I do NOT want to be erased.</h1>
    
    <a href="http://google.com" target="_blank">Total: </a>
    <script type="text/javascript">
    product(3,4);
    document.write(display());
    </script>
    
    </body>
    </html>
    I know you can just return in the first function, "product()" but I would like to do something else in my second function "display()" then return a value.

    thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I take issue with much of what's going on here. But to demonstrate the answer with as little modification of the original code as possible:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function product(a,b){
       var total = a*b;
       return display(total);
    }
    
    function display(num){
       return '<span style="color: red;">' + num + '<\/span>';
    }
    </script>
    </head>
    
    <body>
    <h1>This text I do NOT want to be erased.</h1>
    
    <a href="http://google.com" target="_blank">Total: </a>
    <script type="text/javascript">
    document.write(product(3,4));
    </script>
    
    </body>
    </html>
    As you can see, display() is being run from product, adding the red span wrapper to the mathematical result.

    As a side note, call is technically a different procedure. It's a process whereby the this keyword (if used) of a called function is predefined or set while optionally passing it other arguments:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function product(a,b){
       var total = a*b;
       return display.call(total, 'red');
    }
    
    function display(color){
       return '<span style="color: ' + color + ';">' + this + '<\/span>';
    }
    </script>
    </head>
    
    <body>
    <h1>This text I do NOT want to be erased.</h1>
    
    <a href="http://google.com" target="_blank">Total: </a>
    <script type="text/javascript">
    document.write(product(3,4));
    </script>
    
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •