how to correctly call a function

Discussion in 'Javascript' started by finerrecliner@gmail.com, Aug 16, 2006.

  1. Guest

    what i'm trying to accomplish:
    user clicks button. then can click 2 more times anywhere on the page
    and display his mouse coordinates. after those 2 clicks, go back to
    normal mouse behavior

    the code below does exactly that, but i dont understand how my click
    function got the event argument, since i never passed anything to
    it(see denoted line)? why does this work?

    note: if i use the line document.onclick=click(e);
    it won't work correctly, and will only call the click function when the
    user clicks the button (rather than anywhere on the page).

    <html>
    <head>
    <script type="text/javascript">
    var x = 0;
    function getcoords(e)
    {
    if (!e) var e = window.event;
    document.onclick=click; //why does this line work?!
    }
    function click(e)
    {
    if(x == 0)
    {
    x = 1;
    }
    else if(x == 1)
    {
    alert(e.clientX + " , " + e.clientY);
    x = 2;
    }
    else if(x == 2)
    {
    alert(e.clientX + " , " + e.clientY);
    x = 0;
    document.onclick=null;
    }
    else { alert("wtf");}

    }
    </script>
    </head>
    <body>
    <button onclick="getcoords(event);">click here</button>
    </body>
    </html>
     
    , Aug 16, 2006
    #1
    1. Advertising

  2. wrote:


    > note: if i use the line document.onclick=click(e);
    > it won't work correctly, and will only call the click function when the
    > user clicks the button (rather than anywhere on the page).


    Well that assignment calls the click function with an argument e (which
    is probably not defined) and the result of that function call is
    assigned to the document.onclick property.


    > document.onclick=click; //why does this line work?!



    > function click(e)


    An event handler is a function and your assigment correctly assigns a
    function, your click function, to the document.onclick property. The
    browser then, when a click event occurs, calls that function, it is not
    your code that has to call the function. And in the case of Mozilla the
    browser passes in an event object as the first argument of the function.
    For IE you need to use the window.event object as it does not pass in
    the event object when it calls an event handler.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 16, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. grbgooglefan
    Replies:
    2
    Views:
    433
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    449
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    404
    grbgooglefan
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    258
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    196
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page