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/