Connecting Tech Pros Worldwide Help | Site Map

Add EventListener to <a href...> tag

Fabio Cavassini
Guest
 
Posts: n/a
#1: Dec 15 '05
I want to clean my html from JavaScript and attach all EventHanding
code as:

document.getElementById('to').addEventListener('on Mouseout',delayhidemenu,false);
document.getElementById('s').addEventListener('onM ouseout',delayhidemenu,false);

that way, I can work with a clean HTML page and a separate JavaScript
file.

But, I can't make it work with links, having:

<SCRIPT LANGUAGE="JavaScript">
window.onload=function(){
document.getElementById('h').addEventListener('onC lick',say,false);
}
function say(){
alert("hello");
}
</SCRIPT>
<a id="h" href="#">hhh</a>

Produces the error: The object doesn't accept this property or method.

How can I add an event handler to a <a> tag?

Best Regards
Fabio Cavassini

Rob Williscroft
Guest
 
Posts: n/a
#2: Dec 15 '05

re: Add EventListener to <a href...> tag


Fabio Cavassini wrote in
news:1134680776.473954.70810@g44g2000cwa.googlegro ups.com in
comp.lang.javascript:
[color=blue]
> I want to clean my html from JavaScript and attach all EventHanding
> code as:
>
> document.getElementById('to').addEventListener('on Mouseout',delayhideme
> nu,false);
> document.getElementById('s').addEventListener('onM ouseout',delayhidemen
> u,false);
>
> that way, I can work with a clean HTML page and a separate JavaScript
> file.
>
> But, I can't make it work with links, having:
>
> <SCRIPT LANGUAGE="JavaScript">
> window.onload=function(){
> document.getElementById('h').addEventListener('onC lick',say,f
> alse);
> }
> function say(){
> alert("hello");
> }
> </SCRIPT>
> <a id="h" href="#">hhh</a>
>
> Produces the error: The object doesn't accept this property or method.
>
> How can I add an event handler to a <a> tag?
>[/color]

<script type="text/javascript">
window.addEventListener(
'load',
function()
{
document.getElementById('h').addEventListener('cli ck',say,false);
},
false
);

function say()
{
alert("hello");
}
</script>

Note the lack of an 'on' prefix to the event names.

Internet Explorer will fail with the above as it (currently)
lacks addEventListener, it dose have a proprietry attachEvent
though:

<script type="text/javascript">
function add_listener( on, name, func )
{
if ( on.addEventListener )
{
on.addEventListener(
name.replace( /^on/, ''), func, false
);
}
else if ( on.attachEvent )
{
on.attachEvent( name, func );
}
else
{
on[name] = func;
}
}

add_listener(
window, 'onload',
function()
{
add_listener( document.getElementById('h'), 'onclick', say );
}
);
</script>

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Fabio Cavassini
Guest
 
Posts: n/a
#3: Dec 15 '05

re: Add EventListener to <a href...> tag


Work perfect, thanks ;)

Year 2005 and still we have to take care of different JavaScript
browser implementations...that's embarrassing....

Regards
Fabio Cavassini

Chris Lieb
Guest
 
Posts: n/a
#4: Dec 15 '05

re: Add EventListener to <a href...> tag



Fabio Cavassini wrote:
[snip][color=blue]
> Year 2005 and still we have to take care of different JavaScript
> browser implementations...that's embarrassing....[/color]

You have to remember that IE is still stuck back in 2001, while other
browsers like Firefox and Opera are in continual development, which
allows them to keep up with the standards.

Chris Lieb

Fabio Cavassini
Guest
 
Posts: n/a
#5: Dec 22 '05

re: Add EventListener to <a href...> tag


Is it possible to add parameters to this?

For example:

add_listener( document.getElementById('h'), 'onclick',
say(someStaticParameter) );

or

add_listener( document.getElementById('h'), 'onclick',
say(document.getElementById('h')) );

Best Regards
Fabio Cavassini
http://www.pldsa.com

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Dec 22 '05

re: Add EventListener to <a href...> tag


Fabio Cavassini wrote:
[color=blue]
> Is it possible to add parameters to this?[/color]

To /what/?

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
<URL:http://www.safalra.com/special/googlegroupsreply/>
[color=blue]
> For example:
>
> add_listener( document.getElementById('h'), 'onclick',
> say(someStaticParameter) );
>
> or
>
> add_listener( document.getElementById('h'), 'onclick',
> say(document.getElementById('h')) );[/color]

I do not understand the question. What is add_listener(), what is say()?


PointedEars
Jonas Raoni
Guest
 
Posts: n/a
#7: Dec 22 '05

re: Add EventListener to <a href...> tag


> Is it possible to add parameters to this?

You'll need to make some tricks to achieve this...

Something like works fine:

getHandler = function getHandler(arg1, arg2){
return function(e){
alert([arg1, arg2].join("\n"));
};
}

addEvent(document, "mousedown", getHandler("Mouse was pressed out",
"mousedown"));
addEvent(document, "mouseup", getHandler("Mouse was released",
"mouseup"));

But if you need to remove the handler later, you'll need to do
something like:

addEvent(document, "mousedown", x = getHandler("Mouse was pressed out",
"mousedown"));
addEvent(document, "mouseup", y = getHandler("Mouse was released",
"mouseup"));

:

removeEvent(document, "mousedown", x);
removeEvent(document, "mouseup", y);


--
"Invente, Tente!!! Faça um código eficiente" (eu)

Jonas Raoni Soares Silva
---------------------------
jonasraoni at gmail dot com
http://www.jsfromhell.com

Rob Williscroft
Guest
 
Posts: n/a
#8: Dec 25 '05

re: Add EventListener to <a href...> tag


Fabio Cavassini wrote in news:1135267741.289210.81150
@z14g2000cwz.googlegroups.com in comp.lang.javascript:
[color=blue]
> Is it possible to add parameters to this?
>
> For example:
>
> add_listener( document.getElementById('h'), 'onclick',
> say(someStaticParameter) );
>
> or
>
> add_listener( document.getElementById('h'), 'onclick',
> say(document.getElementById('h')) );
>[/color]

A closeure would solve the problem:

add_listener(
document.getElementById('h'),
'onclick',
function ()
{
say(someStaticParameter);
}
);

If you want to capture the value that "someStaticParameter"
has at the point you call add_listener rather than the value
it will have at some point in the future when the event gets
fired, then you need another level of indirection:

function add_listner_capture_arg( obj, name, f, arg )
{
add_listener(
obj,
name,
function ()
{
f( arg );
}
);
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Fabio Cavassini
Guest
 
Posts: n/a
#9: Dec 26 '05

re: Add EventListener to <a href...> tag


Thanks Rob!!!

It works perfectly, again

Thanks, slowly I'm getting taste of the flexibility of JavaScript....

Best Regards

Closed Thread