Re: Handle events from central script
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Handle events from central script

From: Markus Ernst <derernst@NO#SP#AMgmx.ch>
Date: Thu Jun 23 2005 - 17:37:00 CEST

Michael Winter wrote:
>
> [snip]
>
> Closures do not take a 'snapshot' of the variables in their scope
> chain, but remain live. All of these functions will share the same
> element value, and that value will always be the last A element in the
> collection at the end of the loop. The solution is simple though, and
> is more efficient, too:
>
> function setAktiv() {
> var pN = this.parentNode;
>
> if(pN) {pN.className = 'aktiv';}
> }
> function setPassiv() {
> var pN = this.parentNode;
>
> if(pN) {pN.className = 'passiv';}
> }
>
> if(document.getElementsByTagName) {
> var a = document.getElementsByTagName('a'),
> i = a.length;
>
> while(--i) {
> a[i].onmouseover = a[i].onfocus
> = setAktiv;
> a[i].onmouseout = a[i].onblur
> = setPassiv;
> }
> }

This is really amazing, thanks a lot!

> By the way, if you didn't mean to ignore the first A element in the
> collection (arrays and collections use zero-based indices), then
> change the while condition to a postfix decrement: i--

I actually meant to leave out index 0, anyway it is a better solution to
only address the links below a specified main container (such as navigation,
contents or whatever). So my final solution can be called with a list of
containers:
<body onLoad="aktiviereLinks('navigation', 'contents')>

That's the script:

function machAktiv() {
    var pN = this.parentNode;
    if (pN) {
        var klasse = "aktiv";
        if (pN.className.indexOf("Unten") != -1) klasse = klasse + "Unten";
        pN.className = klasse;
    }
}
function machPassiv() {
    var pN = this.parentNode;
    if (pN) {
        var klasse = "passiv";
        if (pN.className.indexOf("Unten") != -1) klasse = klasse + "Unten";
        pN.className = klasse;
    }
}
function aktiviereLinks() {
    if (document.getElementsByTagName) {
        args = aktiviereLinks.arguments;
        j = args.length;
        while (j--) {
            a = document.getElementById(args[j]).getElementsByTagName("a");
            i = a.length;
            while (i--) {
                a[i].onmouseover = a[i].onfocus = machAktiv;
                a[i].onmouseout = a[i].onblur = machPassiv;
            }
        }
    }
}

Thank you very much for your great inputs, I learnt a lot today!

-- 
Markus 
Received on Tue Oct 18 02:47:56 2005