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
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry What is wrong with this script??

    Hello all , I just start my self-lessons on javascript, it is pretty cool, but i have some trouble understanding some , can any one tell me what's wrong with this code, i would like when the user press the button the windows pop-ups.

    the code:

    <html>
    <head>
    <title>Hello World</title>
    <script type="text/javascript" src="script.js"></script>
    </head>

    <body>
    <form action="#">
    <input type="button" value="Click Me!" id="hibut" />
    </form>
    </body>

    </html>


    --------------------------------------------

    function Hibye(){
    window.alert('Hello...');
    window.alert('Bye');
    }

    var hi_button = document.getElementById('hibut');
    hi_button.onclick = Hibye();

    Thanks........

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Two major problems

    1. onclick is an event handler. As such it needs to be assigned to a function/method. You assign it to the result(!) of the execution of the function/method Hibye. You'll have to learn the difference between both
    Code:
    // function/method definition
    function Hibye() {
       ...
       return "something";
    }
    
    var myFunc = Hibye;  // assignment of a function object to the variable myFunc
    var myFuncResult = Hibye();   // assignment of the result (the return value) == "something"
    onclick needs to be assigned to a function object!

    2. If you try to access an element of the DOM you need to make sure that the DOM has already been loaded. The easiest way to achieve this is to put all your code accessing elements of the DOM into a function that is only executed after the DOM has finished loading
    Code:
    window.onload = function() {
       var hi_button = document.getElementById('hibut');
       hi_button.onclick = Hibye;
    };

  • Users who have thanked devnull69 for this post:

    2mato (07-10-2011)

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks ,,,,,

    But this example is in the book I am learning from

    I will but it in the , can U please send my a title or a link that i can learn JavaScript in a proper way ,,,,

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Which book is it? Are you sure you copied it literally? I am pretty sure that it's not in a book that way.

  • #5
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Which book is it? Are you sure you copied it literally? I am pretty sure that it's not in a book that way.
    JavaScript: A Beginner’s Guide Third Edition

    Page 149

    I copy it as it is , do you have a better resource for learning javaScript?

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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