By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

want to make an action listener in javascript

P: n/a
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 14 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
fi***********@gmail.com 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
Aug 14 '06 #2

P: n/a
fi***********@gmail.com 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.

Aug 15 '06 #3

P: n/a
bobzimuta said the following on 8/14/2006 8:32 PM:
fi***********@gmail.com 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/
Aug 15 '06 #4

P: n/a
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
Aug 15 '06 #5

P: n/a
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.

Aug 15 '06 #6

P: n/a

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; }

Aug 15 '06 #7

P: n/a
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/
Aug 15 '06 #8

P: n/a
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.

Aug 15 '06 #9

P: n/a
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/
Aug 15 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.