473,395 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

want to make an action listener in javascript

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
9 13594
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
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
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rpesq | last post by:
Hi, I Need help changing the content of a DIV. I sincerely researched the issue and have not found a solution except to scrap the idea and stick with the iframe code that I had been using. My...
7
by: ThunderMusic | last post by:
Hi, I have a CheckBoxList and I want to add some javascript code to each CheckBox created by this CheckBoxList. I tried iterating through all items of the list, all the controls, do a FindControl,...
3
by: darwinist | last post by:
http://darwinist.googlepages.com/htmldesktop.html What's the point of something like this? As a php programmer, everything is already there for me, except a decent client-gui system. I could...
4
by: kidalex | last post by:
I'm a programmer but I have never programmed in Action Script or very advanced JavaScript. So, please be gentle :-) I'm looking to develop a simple ( or is it? ) system with 2 parts to it: ...
1
by: stagedud | last post by:
At this moment im working on a _MouseMove function, and I have a question about my function. When somebody moves his mouse over the checkbox, a different label shows a description from a database....
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryString In ASP, Request.Form and Request.QueryString return objects that do not support "toString", or any JavaScript string...
2
by: thirunavukarasukm | last post by:
Hai.... "i want used confirm in javascript in code behind" i am used confirm button ,,,in code behind. if the confirm button worked the function is return the true...
7
by: kalar | last post by:
i make a JFrame window and i add a label i.e. myLabel , which has an image and down from the label a button i.e. dice. I want when i press the button to change the image on the label but when i am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.