Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listeners not working with onClick

Status
Not open for further replies.

Echilon

Programmer
Feb 22, 2007
54
GB
I've decided it's time I made my site more accessible. I'm trying to get rid of all inline onclick handlers and place them in an external file. I've search around and found the following code from AListApart, which works with onclick. I tried to modify it for mouseovers but Firebug just reports an exception.
Code:
// [URL unfurl="true"]http://www.alistapart.com/articles/popuplinks[/URL]
function listen(event, elem, func) {
    elem = getElem(elem);
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(event,func,false);
    else if (elem.attachEvent)  // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    else throw 'cannot add event listener';
}
function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    return this;
}
function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}
The in the head:
Code:
listen('load', window, function() {
listen('mouseover','trigger1',function1);
listen('mouseover','trigger2',function2);
});
Is there any way I can use onclicks which aren't inline?
 
This shows you how it is done.
[tt]
<html>
<head>
<script language="javascript">
//op's ref function listen(event, elem, func) {
elem = getElem(elem);
if (elem.addEventListener) // W3C DOM
elem.addEventListener(event,func,false);
else if (elem.attachEvent) // IE DOM
elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
// for IE we use a wrapper function that passes in a simplified faux Event object.
else throw 'cannot add event listener';
}
function W3CDOM_Event(currentTarget) {
this.currentTarget = currentTarget;
this.preventDefault = function() { window.event.returnValue = false }
return this;
}
function getElem(elem) {
if (document.getElementById) {
if (typeof elem == "string") {
elem = document.getElementById(elem);
if (elem===null) throw 'cannot get element: element does not exist';
} else if (typeof elem != "object") {
throw 'cannot get element: invalid datatype';
}
} else throw 'cannot get element: unsupported DOM';
return elem;
}
[blue]listen('load', window, function() {
//listen('mouseover','trigger1',function1);
//listen('mouseover','trigger2',function2);
listen('click','x',f)
listen('mouseover','y',f)
}
);
function f(evt) {
var elem=evt.currentTarget;
//these help you see what you have to script functionality
alert(elem.innerHTML);
}[/blue]
</script>
</head>
<body>
<button id="x">click to do something</button><br />
<div id="y" style="background-color:yellow;width:30%">mouseover to show something</div>
</body>
</html>
[/tt]
ps: Though the wish to "make the site more accessible" is irreproachable, there is a thin-line separating sophistication and charlatanism. There is nothing wrong with honest online straightforward event handlers. If the author there reproach javascript link, he better does no javascript to achieve the same and any other part of the page to be consistent.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top