Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with Javascript! - Basics

    Hello fellow coders

    I have a fundamental problem with my code and i don't know what the problem is.

    I've only just started doing java script 1 hour ago

    Here's the problem

    This is in the head

    <script type="text/javascript">

    function displayJavaMsg()
    {
    alert(" - Java is an OOP programming language" + '\n' +
    " - Java creates applications that run in a virtual machine.") ;
    }

    function displayJavaScriptMsg()
    {
    alert(" - Java Script is an OOP scripting language" + '\n' +
    " - JavaScript code is run on a browser only") ;
    }

    function Message(Variable)
    {
    if (Variable == "Java Information")
    {

    return displayJavaMsg()

    }

    else

    {
    return displayJavaScriptMsg()
    }

    }

    </script>

    This is in the body

    <h1>JavaScript_Demonstration</h1>
    <input type="button" onclick="displayJavaMsg()" value="Java"/>
    <input type="button" onclick="displayJavaScriptMsg()" value="Javascript"/>

    <h1>Using a Generic Function</h1>
    <input type="button" onclick="Message()" value="Java Information"/>
    <input type="button" onclick="Message()" value="Javascript Information"/>

    The first part which displays the msgs works fine.

    The 2nd part ( Using Generic Function ) doesn't work

    The task is to enter the onclick code for each button such that each will call the Message function passing information to it so that it will "know" which button has been clicked and thus display the appropriate sentence. So if i clicked Java Information, then display "displayJavaMsg" ELSE "displayJavascriptMsg"

    A little help? Thanks in advanced

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Have you tried using your error console (F12 key)?

    Try:-

    Code:
    <input type="button" onclick="Message(this.value)" value="Java Information"/>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    JavaScript is not an OOP language - it is a prototyping language. It runs in the browser, on the desktop, on the server, and lots of other places.

    Running JavaScript in the browser as an OOP language is only using a small fraction of its capabilities.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,646 Times in 4,608 Posts
    To explain Philip's answer:

    Look carefully at your "generic" solution.

    Specifically, look here:
    Code:
    function Message(Variable)
    and then compare that to this code:
    Code:
    <input type="button" onclick="Message()" value="Java Information"/>
    <input type="button" onclick="Message()" value="Javascript Information"/>
    The point: Your Message function expects *AND DEPENDS UPON* receiving one argument.

    Yet your code, when you invoke the function, does not pass any argument.

    Philip showed you one way of passing an argument. One of the best ways. But there are other ways, of course.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys i have another question..

    How about calculating marks?

    Like..

    Writing a function called marks that will prompt a score between 0 and 100 the button is clicked, pass the score to another function called grades and allow that to work out the grade for that score and return relevant letters? like PASS, CREDIT,D,HD and allow the marks function to display the score and the grade

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Quote Originally Posted by Randomasiandude View Post
    Thanks guys i have another question..

    How about calculating marks?

    Like..

    Writing a function called marks that will prompt a score between 0 and 100 the button is clicked, pass the score to another function called grades and allow that to work out the grade for that score and return relevant letters? like PASS, CREDIT,D,HD and allow the marks function to display the score and the grade
    This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    One thing that might help you is that 99.99% of JavaScript should be attached just before the </body> tag so that the parts of the page it references has already been loaded. Only where the script is potentially swapping to a new page without loading the rest of the current one does it belong in the head.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Quote Originally Posted by felgall View Post
    One thing that might help you is that 99.99% of JavaScript should be attached just before the </body> tag so that the parts of the page it references has already been loaded. Only where the script is potentially swapping to a new page without loading the rest of the current one does it belong in the head.
    While that observation is quite correct, I don't see that it makes the slightest difference in this case, where the script is triggered by a button.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,646 Times in 4,608 Posts
    I think we should also point you to
    http://www.codingforums.com/rules.htm

    And especially rule 1.5

    Yes, you can get *SOME* help with homework. But only after you show us that you have made a *REAL* effort to do the work and you just have a couple of minor problems you can't figure out.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    While that observation is quite correct, I don't see that it makes the slightest difference in this case, where the script is triggered by a button.
    It is simply good practice to always place the script at the bottom unless it actually needs to go at the top.

    Placing it at the top unnecessarily can noticeably slow the loading of the web page in some browsers that block other files from loading until the JavaScript finishes loading (just in case the code contains a document.write statement). While modern browsers and connection speeds get around this anyone still using an old browser on a slow connection may have the page take several seconds longer to actually display the content with even a small script attached at the top of the page instead of the bottom.

    Anyway, why get into bad habits just because it doesn't matter with this particular code.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    I did say "While that observation is quite correct, I don't see that it makes the slightest difference in this case, where the script is triggered by a button." And you seem to agree that it makes no difference and does not matter. So we must agree to, er, agree.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    I did say "While that observation is quite correct, I don't see that it makes the slightest difference in this case, where the script is triggered by a button." And you seem to agree that it makes no difference and does not matter. So we must agree to, er, agree.
    We agree that it makes almost no difference in this particular case.

    My sole reason for pointing it out is that many scripts will break if placed in the head and it is better to get into the habit of placing all JavaScript at the bottom of the page unless it actually needs to go in the head (and of all the scripts I have written I have only had one that needed to go in the head - a framebreaker script).

    It looks like the OP is following the antiquated practice from Netscape 4 and earlier of placing most JavaScript in the head. My comment was intended to point out that the bottom of the page is the better location for scripts.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #13
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, please remove this thread admin.
    Have a good one

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Quote Originally Posted by felgall View Post
    We agree that it makes almost no difference in this particular case.
    You make a distinction which makes no difference.
    "A linguistic or conceptual distinction which is of no practical importance or which has no effect on meaning; a perceived difference where there is no actual difference."

    That is not to say that I in any way disagree with you - but your distinction is too lacking in any meaningful difference to be worth making.
    If I say "You should take care while crossing the road", it adds nothing to say "Yes, and take care while crossing motorways as well".

    Last edited by Philip M; 04-30-2013 at 09:27 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    You make a distinction which makes no difference.  
    It would make at least several hundredths of a second difference in the load time of the page on a fast internet connection and possibly several seconds on a slow internet connection.

    It isn't a linguistic distinction - it is a time difference which for most practical purposes with 10Mb+ internet connections makes no noticeable difference.

    On Netscape 4 with a 300b phone connection the page would be minutes slower in loading with the JavaScript at the top instead of at the bottom.

    I was saying "put all your scripts at the bottom and only move them to the top when necessary so that the script doesn't slow the loading of the page". You were saying that because this particular script will only slow the page by a really small amount so as to not be noticed that the OP doesn't need to learn that JavaScript belongs at the bottom of the page. Next thing they'll be attaching a local copy of JQuery to the top of their page and wonder why there is a significant drop in people staying on their site - because they all give up and go elsewhere because all the pictures they wanted to look at are waiting for the JavaScript to finish downloading before they start downloading.

    If no traffic is coming toward you on the road then does it matter which side you drive on? Using your argument the answer is NO because it makes no difference because there is no oncoming traffic. All I am saying is that you should stay on the correct side of the road regardless of oncoming traffic or not - or in JavaScript terms - JavaScript goes at the bottom of the page.
    Last edited by felgall; 04-30-2013 at 10:48 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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