Handle events from central script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Markus Ernst

    #1

    Handle events from central script

    Hello

    I try to hanlde events centrally for avoiding the need to write four event
    handler attributes to every link. The task is changing the class name of a
    link's containing <div> element. After lots of googling I came to this:

    if (document.getEl ementsByTagName ) {
    a = document.getEle mentsByTagName( "a");
    for (var i=1; i<a.length; i++) {
    var element = a[i];
    element.onmouse over = function() { element.parentN ode.className =
    "aktiv"; };
    element.onMouse Out = function() { element.parentN ode.className =
    "passiv"; };
    element.onFocus = function() { element.parentN ode.className =
    "aktiv"; };
    element.onBlur = function() { element.parentN ode.className =
    "passiv"; };
    }
    }

    The script does not throw any errors neither in IE 6 nor in Firefox, but it
    does only change the last element to aktiv, regardless of which element is
    hovered or unhovered.

    I thought of the a[i] overwriting the element variable in every loop and
    also tried calling a[i] directly, and other things such as
    var element = document.getEle mentById(a[i].id);
    or an for-in-loop; the result is only the same.

    Is my approach totally wrong, or is there anything I am missing?

    Thanks for any hint
    Markus


  • Michael Winter

    #2
    Re: Handle events from central script

    On 23/06/2005 14:24, Markus Ernst wrote:

    [snip]
    [color=blue]
    > if (document.getEl ementsByTagName ) {
    > a = document.getEle mentsByTagName( "a");
    > for (var i=1; i<a.length; i++) {
    > var element = a[i];
    > element.onmouse over = function() { element.parentN ode.className =
    > "aktiv"; };
    > element.onMouse Out = function() { element.parentN ode.className =
    > "passiv"; };
    > element.onFocus = function() { element.parentN ode.className =
    > "aktiv"; };
    > element.onBlur = function() { element.parentN ode.className =
    > "passiv"; };
    > }
    > }[/color]

    The problem is that you're forgetting about case-sensitivity: for
    instance, there is no recognised onMouseOut property, but there is a
    generally recognised onmouseout property. The same goes for onFocus and
    onBlur.

    However, you don't need a script to do what you're attempting. CSS is
    quite capable:

    a:link {
    /* Declarations used by the .passiv class */
    }
    a:focus,
    a:hover {
    /* Declarations used by the .aktiv class */
    }

    Note: IE doesn't support the :focus pseudo-class, though others do.

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Markus Ernst

      #3
      Re: Handle events from central script

      Michael Winter wrote:[color=blue]
      > On 23/06/2005 14:24, Markus Ernst wrote:
      >
      > [snip]
      >[color=green]
      >> if (document.getEl ementsByTagName ) {
      >> a = document.getEle mentsByTagName( "a");
      >> for (var i=1; i<a.length; i++) {
      >> var element = a[i];
      >> element.onmouse over = function() {
      >> element.parentN ode.className = "aktiv"; };
      >> element.onMouse Out = function() {
      >> element.parentN ode.className = "passiv"; };
      >> element.onFocus = function() {
      >> element.parentN ode.className = "aktiv"; };
      >> element.onBlur = function() {
      >> element.parentN ode.className = "passiv"; };
      >> }
      >> }[/color]
      >
      > The problem is that you're forgetting about case-sensitivity: for
      > instance, there is no recognised onMouseOut property, but there is a
      > generally recognised onmouseout property. The same goes for onFocus
      > and onBlur.[/color]

      Thank you very much for your input. The case sensitivity issue does indeed
      solve a part of my problem - now all four events are performed, but still
      always on the last element.
      I think I need to find a way to save a reference to a[i] which is not
      overwritten by the following loop.
      [color=blue]
      > However, you don't need a script to do what you're attempting. CSS is
      > quite capable:[/color]

      Yes I use this for the <a> element itself. The task is to highlight also the
      containing element of this html structure:

      <div class="passiv">
      <a href="koepfe/paolo_tonti.htm l">Paolo Tonti</a>
      <span>Werbung / Konzept / Graphic Design</span>
      </div>

      I don't know if CSS provides a syntax to address the parent element - but I
      am sure that it would not be supported by today's common browsers anyway...

      Of course I have a working solution with four event handlers in every link -
      but I would be happy to find a solution that makes a maximum separation of
      content and presentation.

      --
      Markus


      Comment

      • Michael Winter

        #4
        Re: Handle events from central script

        On 23/06/2005 15:11, Markus Ernst wrote:

        [snip]
        [color=blue]
        > Thank you very much for your input. The case sensitivity issue does indeed
        > solve a part of my problem - now all four events are performed, but still
        > always on the last element.[/color]

        Sorry, I forgot to comment on the functions.

        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.get ElementsByTagNa me) {
        var a = document.getEle mentsByTagName( 'a'),
        i = a.length;

        while(--i) {
        a[i].onmouseover = a[i].onfocus
        = setAktiv;
        a[i].onmouseout = a[i].onblur
        = setPassiv;
        }
        }

        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--

        [snip]
        [color=blue]
        > The task is to highlight also the containing element of this html structure:[/color]

        Sorry. I'm clearly not being very attentive today. :(

        [snip]

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Markus Ernst

          #5
          Re: Handle events from central script

          Michael Winter wrote:[color=blue]
          >
          > [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.get ElementsByTagNa me) {
          > var a = document.getEle mentsByTagName( 'a'),
          > i = a.length;
          >
          > while(--i) {
          > a[i].onmouseover = a[i].onfocus
          > = setAktiv;
          > a[i].onmouseout = a[i].onblur
          > = setPassiv;
          > }
          > }[/color]

          This is really amazing, thanks a lot!
          [color=blue]
          > 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--[/color]

          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="aktivie reLinks('naviga tion', 'contents')>

          That's the script:

          function machAktiv() {
          var pN = this.parentNode ;
          if (pN) {
          var klasse = "aktiv";
          if (pN.className.i ndexOf("Unten") != -1) klasse = klasse + "Unten";
          pN.className = klasse;
          }
          }
          function machPassiv() {
          var pN = this.parentNode ;
          if (pN) {
          var klasse = "passiv";
          if (pN.className.i ndexOf("Unten") != -1) klasse = klasse + "Unten";
          pN.className = klasse;
          }
          }
          function aktiviereLinks( ) {
          if (document.getEl ementsByTagName ) {
          args = aktiviereLinks. arguments;
          j = args.length;
          while (j--) {
          a = document.getEle mentById(args[j]).getElementsBy TagName("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


          Comment

          • Mick White

            #6
            Re: Handle events from central script

            Markus Ernst wrote:

            [snip][color=blue]
            > function aktiviereLinks( ) {
            > if (document.getEl ementsByTagName ) {
            > args = aktiviereLinks. arguments;
            > j = args.length;
            > while (j--) {
            > a = document.getEle mentById(args[j]).getElementsBy TagName("a");
            > i = a.length;
            > while (i--) {
            > a[i].onmouseover = a[i].onfocus = machAktiv;
            > a[i].onmouseout = a[i].onblur = machPassiv;
            > }
            > }
            > }
            > }
            >[/color]

            Couple of things:
            [1] "Function.argum ents" deprecated
            [2] Localize scope of loop variables (i & j)

            See rewritten function below:

            function aktiviereLinks( ) {
            if (document.getEl ementsByTagName ) {

            var j = arguments.lengt h;
            while (j--) {
            a = document.getEle mentById(args[j]).getElementsBy TagName("a");
            var i = a.length;
            while (i--) {
            a[i].onmouseover = a[i].onfocus = machAktiv;
            a[i].onmouseout = a[i].onblur = machPassiv;
            }
            }
            }
            }

            Mick

            Comment

            • Danny

              #7
              Re: Handle events from central script


              Why? what purpose does it serve? What's wrong with CSS :hover pseudo
              class on links? Why not giving onmouseover to the actual Div instead?
              You do have to specify what you need, what you have is waaaay overkill, is
              rather very simple to do event checking over a parent for its children, no
              loop needed.

              Danny

              On Thu, 23 Jun 2005 06:24:31 -0700, Markus Ernst <derernst@NO#SP #AMgmx.ch>
              wrote:
              [color=blue]
              > Hello
              >
              > I try to hanlde events centrally for avoiding the need to write four
              > event
              > handler attributes to every link. The task is changing the class name of
              > a
              > link's containing <div> element. After lots of googling I came to this:
              >
              > if (document.getEl ementsByTagName ) {
              > a = document.getEle mentsByTagName( "a");
              > for (var i=1; i<a.length; i++) {
              > var element = a[i];
              > element.onmouse over = function() { element.parentN ode.className =
              > "aktiv"; };
              > element.onMouse Out = function() { element.parentN ode.className =
              > "passiv"; };
              > element.onFocus = function() { element.parentN ode.className =
              > "aktiv"; };
              > element.onBlur = function() { element.parentN ode.className =
              > "passiv"; };
              > }
              > }
              >
              > The script does not throw any errors neither in IE 6 nor in Firefox, but
              > it
              > does only change the last element to aktiv, regardless of which element
              > is
              > hovered or unhovered.
              >
              > I thought of the a[i] overwriting the element variable in every loop and
              > also tried calling a[i] directly, and other things such as
              > var element = document.getEle mentById(a[i].id);
              > or an for-in-loop; the result is only the same.
              >
              > Is my approach totally wrong, or is there anything I am missing?
              >
              > Thanks for any hint
              > Markus
              >
              >[/color]



              --
              Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

              Comment

              • Markus Ernst

                #8
                Re: Handle events from central script

                Mick White wrote:[color=blue]
                >
                > Couple of things:
                > [1] "Function.argum ents" deprecated
                > [2] Localize scope of loop variables (i & j)
                >[/color]

                Thanks a lot. Now I added a recursive function call to make the hover in all
                elements up to the next <div> element:

                function machAktiv() {
                var pN = this.parentNode ;
                umschalten(pN, "aktiv");
                }
                function machPassiv() {
                var pN = this.parentNode ;
                umschalten(pN, "passiv");
                }
                function umschalten(pN, klasse) {
                if (pN) {
                var kl = klasse;
                if (pN.className.i ndexOf("Unten") != -1) kl = kl + "Unten";
                pN.className = kl;
                if (pN.nodeName.to LowerCase() != "div") umschalten(pN.p arentNode,
                klasse);
                }
                }
                function aktiviereLinks( ) {
                if (document.getEl ementsByTagName ) {
                var j = arguments.lengt h;
                while (j--) {
                a =
                document.getEle mentById(argume nts[j]).getElementsBy TagName("a");
                var i = a.length;
                while (i--) {
                a[i].onmouseover = a[i].onfocus = machAktiv;
                a[i].onmouseout = a[i].onblur = machPassiv;
                }
                }
                }
                }

                So in the following HTML the "person" element and all children will be
                highlighted if one of the links is hovered:

                <body onLoad="aktivie reLinks('head') ">
                <div id="person" class="passiv">
                <img src="myimage.gi f" alt="">
                <p class="passiv">
                My name<br>
                <a href="mailto:ma il@domain.com"> mail@domain.com </a><br>
                <span class="passiv"> <a
                href="http://www.domain.com" >www.domain.com </a></span>
                </p>
                </div>
                </body>

                Thank you all for your help!!
                Markus


                Comment

                Working...