want to make an action listener in javascript

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

  1. Guest

    i'm trying to make an action listener that will display an alert box
    when the user clicks anywhere on the page once, and then go back to
    normal mouse behavior.

    i have this:
    <html><script type="text/javascript">
    document.addEventListener('click',hello(),false);
    function hello()
    {
    alert("hello world!")
    }
    </script></html>

    but all it does is show the alert box upon the page loading, and does
    nothing when i click.

    i only care about it working in firefox 1.5+

    thanks for the help.
     
    , Aug 15, 2006
    #1
    1. Advertising

  2. Jeremy Guest

    wrote:
    > i'm trying to make an action listener that will display an alert box
    > when the user clicks anywhere on the page once, and then go back to
    > normal mouse behavior.
    >
    > i have this:
    > <html><script type="text/javascript">
    > document.addEventListener('click',hello(),false);
    > function hello()
    > {
    > alert("hello world!")
    > }
    > </script></html>
    >
    > but all it does is show the alert box upon the page loading, and does
    > nothing when i click.
    >
    > i only care about it working in firefox 1.5+
    >
    > thanks for the help.
    >


    This is because your call to addEventListener should look like this:

    document.addEventListener('click',hello,false);

    If you include the parentheses ("hello()"), the function is called
    before passing to addEventListener (which is why the alert box comes up
    when the page loads). You want to pass the function itself to
    addEventListener, not the results of the function.

    Jeremy
     
    Jeremy, Aug 15, 2006
    #2
    1. Advertising

  3. bobzimuta Guest

    wrote:
    > i'm trying to make an action listener that will display an alert box
    > when the user clicks anywhere on the page once, and then go back to
    > normal mouse behavior.
    >
    > i have this:
    > <html><script type="text/javascript">
    > document.addEventListener('click',hello(),false);


    Don't forget to include cross-browserness:

    if( document.addEventListener )
    document.addEventListener( 'click', hello, false );
    else if( document.attachEvent )
    document.attachEvent( 'onclick', hello );
    else
    document.onclick = hello;

    Turn this into a function, create the opposite function to remove
    events and you're set.
     
    bobzimuta, Aug 15, 2006
    #3
  4. Randy Webb Guest

    bobzimuta said the following on 8/14/2006 8:32 PM:
    > wrote:
    >> i'm trying to make an action listener that will display an alert box
    >> when the user clicks anywhere on the page once, and then go back to
    >> normal mouse behavior.
    >>
    >> i have this:
    >> <html><script type="text/javascript">
    >> document.addEventListener('click',hello(),false);

    >
    > Don't forget to include cross-browserness:
    >
    > if( document.addEventListener )
    > document.addEventListener( 'click', hello, false );
    > else if( document.attachEvent )
    > document.attachEvent( 'onclick', hello );
    > else
    > document.onclick = hello;
    >
    > Turn this into a function, create the opposite function to remove
    > events and you're set.


    Why make things more difficult than they are?

    document.onclick=hello;

    Unless someone knows of a browser where that doesn't work, that is less
    than 8 years old, then there is no need for anything else.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Aug 15, 2006
    #4
  5. Matt Kruse Guest

    Randy Webb wrote:
    > Why make things more difficult than they are?
    > document.onclick=hello;
    > Unless someone knows of a browser where that doesn't work, that is
    > less than 8 years old, then there is no need for anything else.


    Unless you have two scripts (or might in the future) which both want to
    attach to the onclick event. Then the above would have one overwrite the
    other. Not good.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
     
    Matt Kruse, Aug 15, 2006
    #5
  6. Matt Kruse wrote:
    > Randy Webb wrote:
    >> Why make things more difficult than they are?
    >> document.onclick=hello;
    >> Unless someone knows of a browser where that doesn't work,
    >> that is less than 8 years old, then there is no need for anything
    >> else.

    >
    > Unless you have two scripts (or might in the future) which both
    > want to attach to the onclick event. Then the above would have
    > one overwrite the other. Not good.


    As the presented script had a final - x.onclick = y; - branch it would
    be dangerous to use it to add multiple handlers anyway as any browser
    taking that branch would experience problems. And it could be argued to
    be a worse situation as number of browsers taking that final branch
    would be small, and no example may be included in the test browsers
    used, resulting in the impression that there were no problems following
    from using that function on the same element repeatedly. At least
    Randy's proposal produces code that will exhibit the same behaviour in
    all environments, so an issue with multiple handlers will be evident
    from the moment the second handler is introduced.

    Richard.
     
    Richard Cornford, Aug 15, 2006
    #6
  7. Randy Webb wrote:
    > Why make things more difficult than they are?
    >
    > document.onclick=hello;
    >

    That doesn't address the requirement that it should be called only
    once.

    document.onclick=function(){ alert('Your first click!');
    document.onclick=null; }
     
    Stephen Chalmers, Aug 15, 2006
    #7
  8. Randy Webb Guest

    Stephen Chalmers said the following on 8/15/2006 8:44 AM:
    > Randy Webb wrote:
    >> Why make things more difficult than they are?
    >>
    >> document.onclick=hello;
    >>

    > That doesn't address the requirement that it should be called only
    > once.
    >
    > document.onclick=function(){ alert('Your first click!');
    > document.onclick=null; }
    >


    Very true, but, I wasn't addressing that part of the code. I was
    addressing the efficiency of the code. And yes, I am aware of the
    multiple event handlers issue mentioned by Matt.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Aug 15, 2006
    #8
  9. bobzimuta Guest

    Randy Webb wrote:
    > Why make things more difficult than they are?
    >
    > document.onclick=hello;
    >
    > Unless someone knows of a browser where that doesn't work, that is less
    > than 8 years old, then there is no need for anything else.


    What's so difficult with

    function hello()
    {
    alert( 'hello' );
    removeEvent( document, 'click', hello, false );
    }

    addEvent( document, 'click', hello, false );

    It's clean and follows the DOM 2 spec.
     
    bobzimuta, Aug 15, 2006
    #9
  10. Randy Webb Guest

    bobzimuta said the following on 8/15/2006 2:52 PM:
    > Randy Webb wrote:
    >> Why make things more difficult than they are?
    >>
    >> document.onclick=hello;
    >>
    >> Unless someone knows of a browser where that doesn't work, that is less
    >> than 8 years old, then there is no need for anything else.

    >
    > What's so difficult with
    >
    > function hello()
    > {
    > alert( 'hello' );
    > removeEvent( document, 'click', hello, false );
    > }
    >
    > addEvent( document, 'click', hello, false );
    >
    > It's clean and follows the DOM 2 spec.


    Nothing - if and only if - it is supported.

    Did you test that code in any browsers?

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Aug 16, 2006
    #10
    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. Joe Bloggs
    Replies:
    1
    Views:
    836
    Sudsy
    Aug 3, 2003
  2. rjweytens
    Replies:
    6
    Views:
    16,135
    rjweytens
    Jun 25, 2004
  3. runescience

    Struts mapping action to action???

    runescience, Feb 6, 2006, in forum: Java
    Replies:
    3
    Views:
    1,860
    runescience
    Feb 7, 2006
  4. Ben
    Replies:
    22
    Views:
    1,117
    Chris Uppal
    Feb 17, 2006
  5. K Viltersten
    Replies:
    12
    Views:
    233
    Thomas 'PointedEars' Lahn
    May 23, 2008
Loading...

Share This Page