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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Location
    Miami, Florida
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Newbie Question - How to Make JavaScript work?

    Hello -

    I am just beginning to learn Javascript and have been trying to test the most basic script from an external file:

    Code:
    function a_message() 
    { 
    alert('I came from an external script! Ha, Ha, Ha!!!!'); 
    }
    The filename is 'script.js' and I have linked it in the head of my HTML index file using the following linkage:

    Code:
    <script type="text/javascript" src="script.js"></script>
    I can't get it to work… I know I'm missing something very basic here. Any suggestions?

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by mjr08010 View Post
    Hello -

    I am just beginning to learn Javascript and have been trying to test the most basic script from an external file:

    Code:
    function a_message() 
    { 
    alert('I came from an external script! Ha, Ha, Ha!!!!'); 
    }
    The filename is 'script.js' and I have linked it in the head of my HTML index file using the following linkage:

    Code:
    <script type="text/javascript" src="script.js"></script>
    I can't get it to work… I know I'm missing something very basic here. Any suggestions?
    You must "call" the function before the
    code will run the alert.
    Maybe like this ...
    Code:
    <script type="text/javascript" src="script.js"></script>
     <script type="text/javascript" >
           a_message() 
     </script>
    or like this ...

    Code:
    <html>
    <body>
    <button onclick="a_message()"> click </button>
    </body>
    </html>
    Good luck with your JavaScript studies.
    Last edited by DaveyErwin; 01-02-2014 at 02:58 AM.

  • Users who have thanked DaveyErwin for this post:

    mjr08010 (01-02-2014)

  • #3
    New to the CF scene
    Join Date
    Dec 2013
    Location
    Miami, Florida
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you, DaveyErwin! It worked perfectly.

    When calling the function, must the code below be put in the head of the HTML?

    Code:
    <script type="text/javascript" >
           a_message() 
     </script>

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,213
    Thanks
    23
    Thanked 605 Times in 604 Posts
    Quote Originally Posted by mjr08010 View Post
    ...When calling the function, must the code below be put in the head of the HTML?
    No. The common intelligence wants the javascript to be before the </bod> tag and not in the head.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    </head>
    
    <body>
    
    <button onclick="a_message()"> click </button>
    
    <script type="text/javascript">
    function a_message() {
    	alert('I came from an external script! Ha, Ha, Ha!!!!');
    }
    </script>
    
    </body>
    </html>
    OR:
    Code:
    <body>
    
    <button onclick="a_message()"> click </button>
    
    <script type="text/javascript" src="script.js"></script>
    
    </body>
    </html>
    Last edited by sunfighter; 01-02-2014 at 03:24 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New to the CF scene
    Join Date
    Dec 2013
    Location
    Miami, Florida
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    sunfighter -

    Are you saying the link to the external javascript page should not be in the head of the HTML document?

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Yes, you should follow
    his example and put the
    link just before the </body>
    tag


  •  

    Posting Permissions

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