Forum Moderators: open
I have a link that looks like this...
<a href="/.../" onmouseover="return z('a','b')" onfocus="return z('a','b')" onclick="return z('a','b')" onkeypress="return z('a','b')">...</a>
Is there a way to further optimizie this?
Originally I wanted onmouseover and onclick, but I added onfocus and onkeypress for accessibility concerns.
Since they returns are the same, can I combine them, something like this:
Pesudo Code:
<a href="/.../" on(mouseover¦focus¦click¦keypress)="return z('a','b')">...</a>
In the head of your document:
<script type="text/javascript">
function attachBehaviors() {
var az = document.getElementById('zlink');
if(!az ) return;
az.onmouseover = az.onfocus = az.onclick = az.onkeypress = function(){return z('a','b');};
}
window.onload = attachBehaviors;
</script>
And change your HTML to:
<a href="/.../" id="zlink">...</a>
About the only thing I can offer is that with onmouseover, onclick should be redundant, since one would have to mouseover to click.
timster:
...it sound like that assumes the visitor used a mouse to activate the link.
Rambo Tribble:
Onmouseover and onclick are both mouse events; onfocus would cover the case where a field was tabbed to and the enter key used simulate onclick.
I think timster is correct. The onclick event (on anchors/buttons) is not just a mouse event - it is also fired when a user tabs to a link and activates it with the keyboard. The onfocus event will fire as soon as the link is tabbed to using the keyboard - so in this respect is on a par with the onmouseover event for those using the mouse.
I would have said the onkeypress event on the anchor was superfluous in the original example, since the onclick event would also fire.