How can I show the output a function?

Discussion in 'JavaScript' started by moronic_kaos, Jul 22, 2010.

  1. #1
    I'm trying to just display the output (return result) of a function using document.write, but it doesn't seem to work. Here's a really simple example...

    
    function some_function(abc)
      {
      return abc;
      }
    
    document.write(some_function('Hello world'));
    
    Code (markup):
    Yet if I replace "document.write" with "alert", it works just fine (except I don't want an alert box...)

    
    function some_function(abc)
      {
      return abc;
      }
    
    alert(some_function('Hello world'));
    
    Code (markup):
     
    moronic_kaos, Jul 22, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    It works for me:
    
    <script type="text/javascript">
    function some_function(abc)
      {
      return abc;
      }
    
    document.write(some_function('Hello world'));
    </script>
    
    HTML:
     
    Rainulf, Jul 22, 2010 IP
  3. VariousWays

    VariousWays Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    Try wrapping the code in Try-Catch and see what your browser has to say:

    
    <script language="javascript">
    
    try
    {
        function some_function(abc)
        {
            return abc;
        }
    
        document.write(some_function('Hello world'));
    }
    catch(e)
    {
        alert(e);
    }
    
    </script>
    
    HTML:
     
    VariousWays, Jul 26, 2010 IP
  4. moronic_kaos

    moronic_kaos Well-Known Member

    Messages:
    202
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Solved it with "document.getElementById('divname').innerHTML=function();" & creating labeled div tags.

    Thanks though.
     
    moronic_kaos, Jul 26, 2010 IP