473,511 Members | 15,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event Handler Question

I have this even handler (using prototype.js):

showCommentsLinks[i].observe('click', function(event)
{ alert('hi') });
It's attaching to a link element:

<a id="showCommentsLink" href="comments/">Show Comments</a>

When ever my event handler is called, which can be implied as
javascript is enabled and working, I want to prevent the the browser
from following the href url.

If i wasn't using unobtrusive javascript, I would go like this:

<a id="showCommentsLink" href="comments/" onclick="alert('hi');return
false;">Show Comments</a>'

and that would prevent the browser from going to /comments after the
JS executes. How can I recreate this functionality with the
unobtrusive event handler above?

Apr 11 '07 #1
4 2123
On Apr 12, 9:01 am, "egg...@gmail.com" <egg...@gmail.comwrote:
I have this even handler (using prototype.js):

showCommentsLinks[i].observe('click', function(event)
{ alert('hi') });

It's attaching to a link element:

<a id="showCommentsLink" href="comments/">Show Comments</a>
Just because you are using Prototype does not mean you *must* use
Event.observe. In fact, you are better to only use it where its use
is specifically indicated (which is not very often).

Just have the function return false:

<script type="text/javascript">

function hi(){
alert('hi');
return false;
}

window.onload = function(){
document.getElementById('a00').onclick = hi;
}

</script>

<a href="http://..." id="a00">some link</a>
Or if your function doesn't return false, use:

window.onload = function(){
document.getElementById('a00').onclick = function(){
hi();
return false;
}
}

>
When ever my event handler is called, which can be implied as
javascript is enabled and working, I want to prevent the the browser
from following the href url.

If i wasn't using unobtrusive javascript, I would go like this:

<a id="showCommentsLink" href="comments/" onclick="alert('hi');return
false;">Show Comments</a>'
Most of what is included under the misnomer of 'unobtrusive
javascript' is ill-conceived - unobtrusive to whom? Can you provide a
definition?

Consider the example above. If a user clicks the link before the page
finishes loading, they will navigate to the link because your onload
handler hasn't added the A element's onclick handler yet. But if
you'd added it in-line, it would be there doing its job - which is
less obtrusive?

Maybe that's the effect you want, but the different behaviours may
surprise users.

and that would prevent the browser from going to /comments after the
JS executes.
Only if the page has finished loading and the handler has done its
job.
--
Rob

Apr 12 '07 #2
On Apr 11, 7:10 pm, "RobG" <r...@iinet.net.auwrote:
On Apr 12, 9:01 am, "egg...@gmail.com" <egg...@gmail.comwrote:
I have this even handler (using prototype.js):
showCommentsLinks[i].observe('click', function(event)
{ alert('hi') });
It's attaching to a link element:
<a id="showCommentsLink" href="comments/">Show Comments</a>

Just because you are using Prototype does not mean you *must* use
Event.observe. In fact, you are better to only use it where its use
is specifically indicated (which is not very often).

Just have the function return false:

<script type="text/javascript">

function hi(){
alert('hi');
return false;
}

window.onload = function(){
document.getElementById('a00').onclick = hi;
}

</script>

<a href="http://..." id="a00">some link</a>

Or if your function doesn't return false, use:

window.onload = function(){
document.getElementById('a00').onclick = function(){
hi();
return false;
}
}
When ever my event handler is called, which can be implied as
javascript is enabled and working, I want to prevent the the browser
from following the href url.
If i wasn't using unobtrusive javascript, I would go like this:
<a id="showCommentsLink" href="comments/" onclick="alert('hi');return
false;">Show Comments</a>'

Most of what is included under the misnomer of 'unobtrusive
javascript' is ill-conceived - unobtrusive to whom? Can you provide a
definition?

Consider the example above. If a user clicks the link before the page
finishes loading, they will navigate to the link because your onload
handler hasn't added the A element's onclick handler yet. But if
you'd added it in-line, it would be there doing its job - which is
less obtrusive?

Maybe that's the effect you want, but the different behaviours may
surprise users.
and that would prevent the browser from going to /comments after the
JS executes.

Only if the page has finished loading and the handler has done its
job.

--
Rob
If I set up the event handler by means of:

document.getElementById('a00').onclick = function(){

If any other code attaches tot he onclick the previous code will not
execute.

Apr 12 '07 #3
On Apr 12, 3:03 pm, "egg...@gmail.com" <egg...@gmail.comwrote:
On Apr 11, 7:10 pm, "RobG" <r...@iinet.net.auwrote:
On Apr 12, 9:01 am, "egg...@gmail.com" <egg...@gmail.comwrote:
I have this even handler (using prototype.js):
showCommentsLinks[i].observe('click', function(event)
{ alert('hi') });
It's attaching to a link element:
<a id="showCommentsLink" href="comments/">Show Comments</a>
Just because you are using Prototype does not mean you *must* use
Event.observe. In fact, you are better to only use it where its use
is specifically indicated (which is not very often).
Just have the function return false:
<script type="text/javascript">
function hi(){
alert('hi');
return false;
}
window.onload = function(){
document.getElementById('a00').onclick = hi;
}
</script>
<a href="http://..." id="a00">some link</a>
Or if your function doesn't return false, use:
window.onload = function(){
document.getElementById('a00').onclick = function(){
hi();
return false;
}
}
When ever my event handler is called, which can be implied as
javascript is enabled and working, I want to prevent the the browser
from following the href url.
If i wasn't using unobtrusive javascript, I would go like this:
<a id="showCommentsLink" href="comments/" onclick="alert('hi');return
false;">Show Comments</a>'
Most of what is included under the misnomer of 'unobtrusive
javascript' is ill-conceived - unobtrusive to whom? Can you provide a
definition?
Consider the example above. If a user clicks the link before the page
finishes loading, they will navigate to the link because your onload
handler hasn't added the A element's onclick handler yet. But if
you'd added it in-line, it would be there doing its job - which is
less obtrusive?
Maybe that's the effect you want, but the different behaviours may
surprise users.
and that would prevent the browser from going to /comments after the
JS executes.
Only if the page has finished loading and the handler has done its
job.
--
Rob

If I set up the event handler by means of:

document.getElementById('a00').onclick = function(){

If any other code attaches tot he onclick the previous code will not
execute.
That is an old excuse that doesn't stand up to scrutiny.

In what environment can you safely add as many handlers as you like to
an element, in no particular order, without regard for other handlers
that might already be there? In this specific case you want to stop
propagation - how do you know it's safe to do that if you don't know
what other handlers are there and whether they want the event to
propagate? How do you know the order in which they will be called?

You can only safely add multiple handlers if you know exactly what
each one will do and that order is unimportant. Otherwise, you are
much safer to add one handler that calls all the others as functions
in a known order and context.

In a very few cases you might want to add multiple handlers using
attachEvent and addEventListener, but imagine that is the exception
rather than the rule.

The above does not address the other issues of trying to make
addEventListener and attachEvent behave consistently in different
browsers.
--
Rob

Apr 12 '07 #4
On Apr 12, 12:38 am, "RobG" <r...@iinet.net.auwrote:
On Apr 12, 3:03 pm, "egg...@gmail.com" <egg...@gmail.comwrote:
On Apr 11, 7:10 pm, "RobG" <r...@iinet.net.auwrote:
On Apr 12, 9:01 am, "egg...@gmail.com" <egg...@gmail.comwrote:
I have this even handler (using prototype.js):
showCommentsLinks[i].observe('click', function(event)
{ alert('hi') });
It's attaching to a link element:
<a id="showCommentsLink" href="comments/">Show Comments</a>
Just because you are using Prototype does not mean you *must* use
Event.observe. In fact, you are better to only use it where its use
is specifically indicated (which is not very often).
Just have the function return false:
<script type="text/javascript">
function hi(){
alert('hi');
return false;
}
window.onload = function(){
document.getElementById('a00').onclick = hi;
}
</script>
<a href="http://..." id="a00">some link</a>
Or if your function doesn't return false, use:
window.onload = function(){
document.getElementById('a00').onclick = function(){
hi();
return false;
}
}
When ever my event handler is called, which can be implied as
javascript is enabled and working, I want to prevent the the browser
from following the href url.
If i wasn't using unobtrusive javascript, I would go like this:
<a id="showCommentsLink" href="comments/" onclick="alert('hi');return
false;">Show Comments</a>'
Most of what is included under the misnomer of 'unobtrusive
javascript' is ill-conceived - unobtrusive to whom? Can you provide a
definition?
Consider the example above. If a user clicks the link before the page
finishes loading, they will navigate to the link because your onload
handler hasn't added the A element's onclick handler yet. But if
you'd added it in-line, it would be there doing its job - which is
less obtrusive?
Maybe that's the effect you want, but the different behaviours may
surprise users.
and that would prevent the browser from going to /comments after the
JS executes.
Only if the page has finished loading and the handler has done its
job.
--
Rob
If I set up the event handler by means of:
document.getElementById('a00').onclick = function(){
If any other code attaches tot he onclick the previous code will not
execute.

That is an old excuse that doesn't stand up to scrutiny.

In what environment can you safely add as many handlers as you like to
an element, in no particular order, without regard for other handlers
that might already be there? In this specific case you want to stop
propagation - how do you know it's safe to do that if you don't know
what other handlers are there and whether they want the event to
propagate? How do you know the order in which they will be called?

You can only safely add multiple handlers if you know exactly what
each one will do and that order is unimportant. Otherwise, you are
much safer to add one handler that calls all the others as functions
in a known order and context.

In a very few cases you might want to add multiple handlers using
attachEvent and addEventListener, but imagine that is the exception
rather than the rule.

The above does not address the other issues of trying to make
addEventListener and attachEvent behave consistently in different
browsers.

--
Rob
Here's an example that's handy:

I was using messing around with a validation framework called xf.js
and it was attaching the onsubmit of my form in the way you describe.
However, since it doing it like that it was overwriting any event
handlers I attached to the onsubmit.

Apr 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
49557
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. ...
18
2850
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
2
16975
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
3
3628
by: R Millman | last post by:
under ASP.NET, single stepping in debug mode appears not to stop within event procedures. i.e. 1) Create web page with submit button and event procedure for the click event in the code behind...
13
3479
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
6
8754
by: Joseph Geretz | last post by:
I'm porting a C# Outlook Addin originally engineered as a COM Addin over to use VSTO. I've gotten this to the point where my VSTO Addin installs its Menu items and Toolbar buttons when Outlook...
1
2506
by: tdan | last post by:
I do not know how to get Event.stopObserving() to work in the context I am using it. I am displaying a Color Selection Table and attaching 2 events: 1. onmouseover to display the color to the user...
2
1211
by: wolverine | last post by:
Hi, I am writing a javascript code that parses dom and finds event handlers attached to mouseover events. Then i will replace the existing handler say B() with my own function say A(). When the...
24
55041
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
4
237
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: ...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7349
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,...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.