removing nodes in the window.opener

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ole Noerskov

    removing nodes in the window.opener

    The function below is supposed to remove all childnodes with a name
    that starts with "keywords" in "myform" in the window.opener.

    It works fine if there's one keyword node. But you have to run the
    function several times if there are many keyword nodes.
    Why?

    function removeKeywords( )
    {
    var form_obj = window.opener.d ocument.getElem entById('myform ');
    var num_of_elem = form_obj.childr en.length;
    var count;
    for (count = 0; count<num_of_el em;count++)
    {
    var name = form_obj.childr en[count].name;
    if (name.match(/^keywords.*/))
    {
    form_obj.childr en[count].removeNode();
    }

    }
    }
  • Ivo

    #2
    Re: removing nodes in the window.opener


    "Ole Noerskov" wrote[color=blue]
    > The function below is supposed to remove all childnodes with a name
    > that starts with "keywords" in "myform" in the window.opener.
    > It works fine if there's one keyword node. But you have to run the
    > function several times if there are many keyword nodes.
    > Why?[/color]

    Because the form elements array is shortened and any remaining elements have
    their index numbers updated (become one less) when you remove an element. If
    you remove element number 16, number 17 becomes 16, so you want to check 16
    again.
    [color=blue]
    >
    > function removeKeywords( )
    > {
    > var form_obj = window.opener.d ocument.getElem entById('myform ');
    > var num_of_elem = form_obj.childr en.length;
    > var count;
    > for (count = 0; count<num_of_el em;count++)
    > {
    > var name = form_obj.childr en[count].name;
    > if (name.match(/^keywords.*/))
    > {
    > form_obj.childr en[count].removeNode();[/color]

    count--; // add this line here
    [color=blue]
    > }
    > }
    > }[/color]

    HTH
    Ivo


    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: removing nodes in the window.opener

      Ivo wrote:
      [color=blue]
      > "Ole Noerskov" wrote[color=green]
      >> function removeKeywords( )
      >> {
      >> var form_obj = window.opener.d ocument.getElem entById('myform ');
      >> var num_of_elem = form_obj.childr en.length;[/color][/color]

      The property is "childNodes ", not "children". "children"
      may work in IE, but it is not standards compliant. To
      support older IEs, you could write

      var children = form_obj.childN odes || form_obj.childr en;
      if (children)
      {
      var num_of_elem = children.length ;
      ...
      }

      But then you also need

      var formObj = window.opener.d ocument.forms['myform'];

      instead previously, as IEs below 5.0 do not support the
      W3C DOM, only DOM Level 0 and the IE4 DOM, if that.
      [color=blue][color=green]
      >> var count;[/color][/color]

      That is unnecessary, unless you intend to use `num_of_elem'
      and `count' again in the function. Otherwise write
      [color=blue][color=green]
      >> for (count = 0; count<num_of_el em;count++)[/color][/color]

      for (var count = 0, num_of_elem = form_obj.childN odes.length;
      count < num_of_elem;
      count++)

      instead.
      [color=blue][color=green]
      >> {
      >> var name = form_obj.childr en[count].name;[/color][/color]

      That is not good. You redeclare the variable in every loop.
      I recommend to declare a variable holding the object reference
      in the initialization part of the `for' statement:

      for (var count = 0, e = form_obj.childN odes,
      num_of_elem = form_obj.childN odes.length,
      sName = ""; // see below
      count < num_of_elem;
      count++)

      [color=blue][color=green]
      >> if (name.match(/^keywords.*/))[/color][/color]

      Since you seem not evaluate the matches, RegExp.test() is sufficient
      and it returns a boolean value which avoids paying for type casting.
      [color=blue][color=green]
      >> {
      >> form_obj.childr en[count].removeNode();[/color]
      >
      > count--; // add this line here[/color]

      Considering the above, write

      if ((sName = e[count].name)
      && /^keywords.*/.test(sName))
      {
      form_obj.remove Child(e[count--]);
      }

      instead. AFAIS removeNode() is proprietary[1] (there is apparently a
      typo in the Document interface specification of the W3C DOM Level 2
      Specification, as the Node interface has no such method that could be
      inherited[2]. It is no longer there in W3C DOM Level 3 Core.[3])


      PointedEars
      ___________
      [1]
      <http://msdn.microsoft. com/workshop/author/dhtml/reference/methods/removenode.asp>
      [2] <http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document>
      [3] <http://www.w3.org/TR/DOM-Level-3-Core/core.html#i-Document>

      Comment

      • Richard Cornford

        #4
        Re: removing nodes in the window.opener

        Thomas 'PointedEars' Lahn wrote:[color=blue]
        > Ivo wrote:[color=green]
        >> "Ole Noerskov" wrote[/color][/color]
        <snip>[color=blue][color=green][color=darkred]
        >>> {
        >>> var name = form_obj.childr en[count].name;[/color][/color]
        >
        > That is not good. You redeclare the variable in every loop.[/color]

        Because ECMAScript is not block scoped and performs variable
        instantiation for a function once, as it enters an execution context, it
        would not be correct to say "redeclare the variable in every loop". The
        local variable is declared in code within a loop, but that declaration
        is handled at a function level and only happens once, prior to the
        execution of the function body.

        Following variable instantiation, as the function is executing, the only
        significance of that statement is as an assignment, which is what is
        wanted. The - var - keyword has already been employed to create a
        property of the execution context's Variable object with the name "name"
        and that process will not be repeated no matter how often the (or any)
        statement(s) in which the - var - keyword is used is(are) executed.
        [color=blue]
        > I recommend to declare a variable holding the object reference
        > in the initialization part of the `for' statement:[/color]
        <snip>

        Good style may suggest that variables be declared in a block at the top
        of a function definition, or in the initialisation part of a - for -
        statement, but the location of variable declarations makes no actual
        difference to how ECMAScript executes. Which means that if a criteria of
        compact code was applied, instead of a notion "correct style", then
        first explicitly declaring a local variable in the first statement that
        made an assignment to it may save a few bytes of download without making
        any difference to how the code executed.

        Richard.



        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: removing nodes in the window.opener

          Richard Cornford wrote:
          [color=blue]
          > Thomas 'PointedEars' Lahn wrote:[color=green]
          >> Ivo wrote:[color=darkred]
          >>> "Ole Noerskov" wrote[/color][/color]
          > <snip>[color=green][color=darkred]
          >>>> {
          >>>> var name = form_obj.childr en[count].name;[/color]
          >>
          >> That is not good. You redeclare the variable in every loop.[/color]
          >
          > Because ECMAScript is not block scoped and performs variable
          > instantiation for a function once, as it enters an execution context, it
          > would not be correct to say "redeclare the variable in every loop". The
          > local variable is declared in code within a loop, but that declaration
          > is handled at a function level and only happens once, prior to the
          > execution of the function body.[/color]

          You should tell the people of mozilla.org. Maybe they can explain why

          | var x = 42;
          | for (var i = 0; i < 13; i++)
          | {
          | var x = i;
          | }
          | for (var i = 0; i < 13; i++)
          | {
          | x = i;
          | }

          yields

          | Warning: redeclaration of var x
          | Source File: javascript:var x = 42; for (var i = 0; i < 12; i++) { var x =
          | i; } for (var i = 0; i < 12; i++) { x = i; }
          | Line: 1, Column: 47
          | Source Code:
          | var x = 42; for (var i = 0; i < 12; i++) { var x = i; } for (var i = 0;
          | i < 12; i++) { x = i; }
          |
          | Warning: redeclaration of var i
          | Source File: javascript:var x = 42; for (var i = 0; i < 12; i++) { var x =
          | i; } for (var i = 0; i < 12; i++) { x = i; }
          | Line: 1, Column: 65
          | Source Code:
          | var x = 42; for (var i = 0; i < 12; i++) { var x = i; } for (var i = 0;
          | i < 12; i++) { x = i; }

          in Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8a) Gecko/20040509
          and Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8a) Gecko/20040513
          Firefox/0.8.0+.


          PointedEars

          Comment

          • Richard Cornford

            #6
            Re: removing nodes in the window.opener

            "Thomas 'PointedEars' Lahn" wrote:[color=blue]
            >Richard Cornford wrote:[/color]
            <snip>[color=blue][color=green]
            >> Because ECMAScript is not block scoped and performs variable
            >> instantiation for a function once, as it enters an execution context,
            >> it would not be correct to say "redeclare the variable in every
            >> loop". The local variable is declared in code within a loop, but
            >> that declaration is handled at a function level and only happens
            >> once, prior to the execution of the function body.[/color]
            >
            >You should tell the people of mozilla.org.[/color]

            Why should I? They can read the specification for themselves.
            [color=blue]
            > Maybe they can explain why[/color]
            <snip>[color=blue]
            >| Warning: redeclaration of var x[/color]
            <snip>[color=blue]
            >| Warning: redeclaration of var i[/color]
            <snip>

            If Mozilla's JS engine is re-declaring the variable whenever it executes
            the loop body then it is not an ECMA 262 3rd edition conforming
            implementation (which would be poor from an organisation that likes to
            play on its support for standards).

            But the warnings generated by Mozilla (at least when it is set to
            produce strict warnings) are not appropriate for cross browser scripting
            so it would probably be better to turn them off. That way Mozilla at
            least behaves as if its javascript implementation was ECMA 262
            conforming (which is good enough).

            Richard.


            Comment

            • Matt Kruse

              #7
              Re: removing nodes in the window.opener

              Thomas 'PointedEars' Lahn wrote:[color=blue]
              > You should tell the people of mozilla.org. Maybe they can explain why[color=green]
              >> var x = 42;
              >> for (var i = 0; i < 13; i++)
              >> {
              >> var x = i;
              >> }[/color]
              > yields[color=green]
              >> Warning: redeclaration of var x[/color][/color]

              I'm not sure this is anti-standards, as Richard suggests. It may just be a
              general warning that could very well be helpful.
              For example, if you have a .js include file which does

              var totalCount=4; //or whatever

              and then in your page-level script source, you have another declaration like

              var totalCount=data .length;

              then the two would definitely collide, and it would be helpful to know about
              that.
              Declaring 'var' in more than one place makes the source more confusing, too,
              since obviously it's only needed at the top of a given function.

              As long as mozilla isn't actually treating the x variable inside the loop as
              something different than the x declared as 42, I'd say that's a helpful
              warning message.

              --
              Matt Kruse
              Javascript Toolbox: http://www.mattkruse.com/javascript/


              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: removing nodes in the window.opener

                Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> writes:
                [color=blue]
                > You should tell the people of mozilla.org. Maybe they can explain why
                >
                > | var x = 42;
                > | for (var i = 0; i < 13; i++)
                > | {
                > | var x = i;[/color]
                ....[color=blue]
                >
                > yields
                >
                > | Warning: redeclaration of var x[/color]

                Because you are declaring the same variable twice in the same scope.
                The warning is not related to one of the declarations being inside
                a loop.

                In ECMAScript, it is not illegal to declare the same variable more
                than once, which is why it gives a warning, not an error. I expect
                that they give a warning, because declaring a variable more than once
                gives no extra benefit, and is likely to be a mistake.

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Richard Cornford

                  #9
                  Re: removing nodes in the window.opener

                  Lasse Reichstein Nielsen wrote:[color=blue]
                  > Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> writes:
                  >[color=green]
                  >> You should tell the people of mozilla.org. Maybe they can explain
                  >> why
                  >>
                  >> | var x = 42;
                  >> | for (var i = 0; i < 13; i++)
                  >> | {
                  >> | var x = i;[/color]
                  > ...[color=green]
                  >>
                  >> yields
                  >>
                  >> | Warning: redeclaration of var x[/color]
                  >
                  > Because you are declaring the same variable twice in the same scope.
                  > The warning is not related to one of the declarations being inside
                  > a loop.[/color]
                  <snip>

                  I didn't really look at Tomas' s code, I rather foolishly assumed that
                  his example would mirror the code that was the subject of his comments,
                  which did not have multiple declarations of the same variable. Under the
                  circumstances the warning is reasonable (as a warning), and much as I
                  would expect from any lint program. Unfortunately, knowing that Mozilla
                  issues bogus warnings leaves me more inclined to consider any warnings
                  it generates as suspect.

                  Richard.


                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: removing nodes in the window.opener

                    Lasse Reichstein Nielsen wrote:
                    [color=blue]
                    > Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> writes:[color=green]
                    >> You should tell the people of mozilla.org. Maybe they can explain why
                    >> | var x = 42;
                    >> | for (var i = 0; i < 13; i++)
                    >> | {
                    >> | var x = i;[/color]
                    > ...[color=green]
                    >>
                    >> yields
                    >>
                    >> | Warning: redeclaration of var x[/color]
                    >
                    > Because you are declaring the same variable twice in the same scope.
                    > The warning is not related to one of the declarations being inside
                    > a loop. [...][/color]

                    Full ACK. Somehow I overlooked that all the time. Thanks!


                    PointedEars

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: removing nodes in the window.opener

                      Richard Cornford wrote:
                      [color=blue]
                      > Lasse Reichstein Nielsen wrote:[color=green]
                      >> Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> writes:[color=darkred]
                      >>> | Warning: redeclaration of var x[/color]
                      >>
                      >> Because you are declaring the same variable twice in the same scope.
                      >> The warning is not related to one of the declarations being inside
                      >> a loop.[/color]
                      > <snip>
                      >
                      > I didn't really look at Tomas' s code, I rather foolishly assumed that
                      > his example would mirror the code that was the subject of his comments,
                      > which did not have multiple declarations of the same variable. [...][/color]

                      Let him who is without bug cast the first byte ...


                      PointedEars

                      P.S.: It's *Thomas*.

                      Comment

                      • Richard Cornford

                        #12
                        Re: removing nodes in the window.opener

                        Thomas 'PointedEars' Lahn wrote:[color=blue]
                        > Richard Cornford wrote:[/color]
                        <snip>[color=blue][color=green]
                        >> I didn't really look at Tomas' s code, I rather foolishly assumed ...[/color][/color]
                        <snip>[color=blue]
                        > Let him who is without bug cast the first byte ...[/color]

                        :)

                        <snip>[color=blue]
                        > P.S.: It's *Thomas*.[/color]

                        Yes, sorry, its a typo (two actually).

                        Richard.


                        Comment

                        • Ole Noerskov

                          #13
                          Re: removing nodes in the window.opener

                          As usual I found the solution myself before my message even appeared
                          here:
                          Before deleting any nodes I first read in the names or ids in an array
                          - then I use a loop and getElementByNam e or getElementById to remove
                          the nodes.

                          Thanks anyway.
                          It's a little bit funny that most of the replies focus on the
                          javascript style - not my concrete problem. The script was written
                          very quickly just to give an example.

                          Ole Noerskov


                          olenoerskov@hot mail.com (Ole Noerskov) wrote in message news:<582c27ec. 0405262303.d980 963@posting.goo gle.com>...[color=blue]
                          > The function below is supposed to remove all childnodes with a name
                          > that starts with "keywords" in "myform" in the window.opener.
                          >
                          > It works fine if there's one keyword node. But you have to run the
                          > function several times if there are many keyword nodes.
                          > Why?
                          >
                          > function removeKeywords( )
                          > {
                          > var form_obj = window.opener.d ocument.getElem entById('myform ');
                          > var num_of_elem = form_obj.childr en.length;
                          > var count;
                          > for (count = 0; count<num_of_el em;count++)
                          > {
                          > var name = form_obj.childr en[count].name;
                          > if (name.match(/^keywords.*/))
                          > {
                          > form_obj.childr en[count].removeNode();
                          > }
                          >
                          > }
                          > }[/color]

                          Comment

                          Working...