Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Calling a javascript function Problem

  • 01-03-2007 1:21am
    #1
    Registered Users, Registered Users 2 Posts: 1,559 ✭✭✭


    I dont know how to use this java script function within a html page.

    <script type = "text/javascript">
    function Prod(a,b)
    {
    x= a*b;
    return x;

    }


    </script>

    I tried

    <html>
    <head>
    <script type = "text/javascript">
    function Prod(a,b)
    {
    x= a*b;
    return x;

    }


    </script>

    </head>
    <body>

    <script type ="text/javascript">
    var a =10;
    var b=20;
    c = prod(10,20)
    document.write("HEYY"+ c)

    </script>



    </body>
    </html>


    How do I get an html Page to display the result of the function ie. to display the product of two numbers from a javascript function?


Comments

  • Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭smcelhinney


    Small point quinn. Use the code brackets when displaying code, its easier for us to read.

    Is this script not working? It may work in IE because ie being non-standards based is a little forgiving when it comes to XML and javascript. If the code above appears like that in the browser then there are a couple of issues

    1) HTML attributes shouldnt have whitespace. eg. <script type="text/javascript">
    2) You should declare (var) all variables. eg var c = prod(10,20), otherwise, how is the compiler supposed to know what they are? cmon..
    3) Javascript is case sensitive. eg prod(a,b) is NOT the same as Prod(a,b)
    4) Terminate all javascript code blocks with a semicolon. eg. document.write("HEYY"+c);

    If you have copied and pasted this from your code, then the above 4 reasons will (depending on your browser) cause your script to fail. If you try it all within one code block on the page as such.
    <script type = "text/javascript">
    
    function prod(a,b) {
    var x= a*b;
    return x;
    }
    
    var a = 10;
    var b = 20;
    var c = prod(a,b); // why did you have prod(10,20) here?! makes no sense!
    document.write("HEYY"+ c);
    
    </script> 
    

    HTH,
    Stephen


  • Closed Accounts Posts: 884 ✭✭✭NutJob


    Use the Dom to append it to a Div


    eg

    [php]


    <script type = "text/javascript">

    function WriteText(){
    var output = "this should work";

    document.getElementById("Output").innerText = output;
    }


    window.onload = WriteText();
    }


    <BODY>
    .
    .. lots of html type stuff
    ...

    <DIV id="Output"></div>
    [/php]


    2) You should declare (var) all variables. eg var c = prod(10,20), otherwise, how is the compiler supposed to know what they are? cmon..

    Itll work its just not a great idea. Simply because javascript excutes as the doc is read. Use Onload as this allows the documet to finish loading before running javascript so that it doesnt get funny errors if tags havnt loaded in time.


    If your learning javascript download

    http://www.getfirebug.com/
    http://www.mozilla.org/projects/venkman/
    http://chrispederick.com/work/webdeveloper/

    thell help alot


  • Registered Users, Registered Users 2 Posts: 1,559 ✭✭✭quinnd6


    Excellent thanks guys for all the info.
    It works now after your help
    The Problem was I was calling prod instead of Prod(I didnt spot that myself).
    Thanks


Advertisement