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

Newbie Javascript Question

Options
  • 26-05-2009 4:20pm
    #1
    Registered Users Posts: 11


    Newbie to Javascript so apologies.

    If I have a JSP and within in it I want to use functions, for example:
    <html>
       <head>
       </head>
    
       <body>
          <script type="text/javascript">
           int v = 64;
               int c = 8;
    
              document.write(v + " divided by " + c + " = " + divide(v,c));
    
    
              function divide(a,b){
                 return a/b;
               }
    
               
         </script>
       </body>
    </html>
    
    Is this correct?

    Its the functionality moreso than the exact syntax above Im looking to clarify, as I will use it in a more complex JSP.

    Thanks


Comments

  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Ok a number of points:
    1. That's not a JSP page - you have no java code at all there - java and javascript are two very different things
    2. The code as written - I doubt it will work - you've defined your function after using it - that's like expecting a baby to be able to skip rope before it's learnt how to crawl :)
    3. It's not a very useful piece of code even if the above issues were sorted - the values for v and c are hard-coded (that is they can't change unless you physically go in and rewrite this page) - try providing some text fields for the user to put in their values so that at least the page can be reused for numbers other than the ones you've provided

    Hope that helps a bit

    Regards,
    RD


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Ok a number of points:
    1. That's not a JSP page - you have no java code at all there - java and javascript are two very different things
    2. The code as written - I doubt it will work - you've defined your function after using it - that's like expecting a baby to be able to skip rope before it's learnt how to crawl :)
    3. It's not a very useful piece of code even if the above issues were sorted - the values for v and c are hard-coded (that is they can't change unless you physically go in and rewrite this page) - try providing some text fields for the user to put in their values so that at least the page can be reused for numbers other than the ones you've provided

    Hope that helps a bit

    Regards,
    RD
    He says he has a jsp page with javascript in it. No reason to think that it isn't a jsp page, we have no idea of how that page is being rendered.

    That code will work fine, it doesn't matter where the function is defined, before of after, that's how most languages work.

    With regards to the code, it looks like he's testing something rather than looking to return '8' every time...

    OP nothing wrong there with the syntax. Should work fine. You could prob use var instead of int.


  • Registered Users Posts: 10,636 ✭✭✭✭28064212


    You should use var instead of int.
    This ^. Won't display without it on Firefox anyway.

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Moderators, Science, Health & Environment Moderators Posts: 8,954 Mod ✭✭✭✭mewso


    Just remember a few things about javascript.

    1. Try and make your page degrade gracefully. i.e. theres something there if the user hasn't got javascript.
    2. Try and put your script in an external file.
    3. Whats the doctype of this page. I don't think people realise that certain javascript is not legal in xhtml strict like the document.wrtie and innerHTML function etc. Sadly as most people are serving their xhtml strict as text/html they are never made aware of this problem but when all browsers accept the correct content type of xml a lot of people out there may switch to xml and suddenly their script will stop working.

    XHTML when properly served is xml and as such proper DOM manipulation is the recommended way to manipulate your content as well as pretty much the only way. So an example of how I would do this:-
    <html>
       <head>
          <script type="text/javascript" src="js/myscript.js"></script>
       </head>
    
       <body>
          
       </body>
    </html>
    

    and in myscript.js
    
    function initPage()   {
       var v = 64;
       var c = 8;
       var result = divide(v,c);
       var content = document.createTextNode(v + " divided by " + c + " = " + result);
       document.body.appendChild(content);
    }
    
    function divide(a,b){
       return a/b;
    }
    
    window.onload = initPage;
    

    It should be noted I would never use a basic window.onload assignment but it serves as an example here. jQuery's $(document).ready or an alternative way of adding 1 to many load events is best.


Advertisement