473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.document.getElementById('myform');
var num_of_elem = form_obj.children.length;
var count;
for (count = 0; count<num_of_elem;count++)
{
var name = form_obj.children[count].name;
if (name.match(/^keywords.*/))
{
form_obj.children[count].removeNode();
}

}
}
Jul 23 '05 #1
12 1812
Ivo

"Ole Noerskov" wrote
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?
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.

function removeKeywords()
{
var form_obj = window.opener.document.getElementById('myform');
var num_of_elem = form_obj.children.length;
var count;
for (count = 0; count<num_of_elem;count++)
{
var name = form_obj.children[count].name;
if (name.match(/^keywords.*/))
{
form_obj.children[count].removeNode();
count--; // add this line here
}
}
}


HTH
Ivo
Jul 23 '05 #2
Ivo wrote:
"Ole Noerskov" wrote
function removeKeywords()
{
var form_obj = window.opener.document.getElementById('myform');
var num_of_elem = form_obj.children.length;
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.childNodes || form_obj.children;
if (children)
{
var num_of_elem = children.length;
...
}

But then you also need

var formObj = window.opener.document.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.
var count;
That is unnecessary, unless you intend to use `num_of_elem'
and `count' again in the function. Otherwise write
for (count = 0; count<num_of_elem;count++)
for (var count = 0, num_of_elem = form_obj.childNodes.length;
count < num_of_elem;
count++)

instead.
{
var name = form_obj.children[count].name;
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.childNodes,
num_of_elem = form_obj.childNodes.length,
sName = ""; // see below
count < num_of_elem;
count++)

if (name.match(/^keywords.*/))
Since you seem not evaluate the matches, RegExp.test() is sufficient
and it returns a boolean value which avoids paying for type casting.
{
form_obj.children[count].removeNode();


count--; // add this line here


Considering the above, write

if ((sName = e[count].name)
&& /^keywords.*/.test(sName))
{
form_obj.removeChild(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>
Jul 23 '05 #3
Thomas 'PointedEars' Lahn wrote:
Ivo wrote:
"Ole Noerskov" wrote <snip>
{
var name = form_obj.children[count].name;

That is not good. You redeclare the variable in every loop.


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.
I recommend to declare a variable holding the object reference
in the initialization part of the `for' statement:

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

Jul 23 '05 #4
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
Ivo wrote:
"Ole Noerskov" wrote <snip> {
var name = form_obj.children[count].name;


That is not good. You redeclare the variable in every loop.


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.


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
Jul 23 '05 #5
"Thomas 'PointedEars' Lahn" wrote:
Richard Cornford wrote: <snip>
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.


You should tell the people of mozilla.org.


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

<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.
Jul 23 '05 #6
Thomas 'PointedEars' Lahn wrote:
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;
}

yields
Warning: redeclaration of var x


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/
Jul 23 '05 #7
Thomas 'PointedEars' Lahn <Po*********@nurfuerspam.de> writes:
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; ....
yields

| Warning: redeclaration of var x


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #8
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@nurfuerspam.de> writes:
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;

...

yields

| Warning: redeclaration of var x


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.

<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.
Jul 23 '05 #9
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@nurfuerspam.de> writes:
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;

...

yields

| Warning: redeclaration of var x


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. [...]


Full ACK. Somehow I overlooked that all the time. Thanks!
PointedEars
Jul 23 '05 #10
Richard Cornford wrote:
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@nurfuerspam.de> writes:
| Warning: redeclaration of var x


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.

<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. [...]


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

P.S.: It's *Thomas*.
Jul 23 '05 #11
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote: <snip>
I didn't really look at Tomas' s code, I rather foolishly assumed ...

<snip> Let him who is without bug cast the first byte ...
:)

<snip> P.S.: It's *Thomas*.


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

Richard.
Jul 23 '05 #12
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 getElementByName 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
ol*********@hotmail.com (Ole Noerskov) wrote in message news:<58*************************@posting.google.c om>...
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.document.getElementById('myform');
var num_of_elem = form_obj.children.length;
var count;
for (count = 0; count<num_of_elem;count++)
{
var name = form_obj.children[count].name;
if (name.match(/^keywords.*/))
{
form_obj.children[count].removeNode();
}

}
}

Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Renuka | last post by:
I have a confirmation page with an OK button. If this window has an opener then this browser window needs to go to another URL or else the window must close. Thus I have the following javascript...
1
by: fogwolf | last post by:
First a basic outline of what I am trying to do: I want to have a page spawn a pop-up when you click "submit" on its form. On this pop-up page there will be another form. When you click "submit"...
2
by: Robert Nurse | last post by:
Hi All, I'm trying to alter the contents of a drop-down (select) list on the parent window from a child window in IE 6. After opening the child window, I set its opener to reference the parent...
2
by: JPL Verhey | last post by:
(i hope somebody (else) will read and have an idea! Thnx) Hi, With a script in a popup window, I want to check if certain content is present in a page loaded into the frame "main" of the...
19
by: Darren | last post by:
I have a page that opens a popup window and within the window, some databse info is submitted and the window closes. It then refreshes the original window using window.opener.location.reload(). ...
5
by: Hemanth | last post by:
Hello there, I'm running a script that opens a popup window (which is basically a form with checkboxes and a submit button). When I click the submit button I want to run a PHP script and target...
4
by: louise raisbeck | last post by:
I have this scenario (simplified) function addnewdata () { check for partial match already in db for information entered by user if (partialmatch succeeds) { open new window aspx page (using...
5
by: asadhkhan | last post by:
I have the following code which works correctly in IE 6, but in IE 7, Fire Fox 2.0 and Netscape 8 it does not work. I have a main page where a button calls this pop-up and uploads a file once you...
1
by: jerkyjerk | last post by:
Ei guys..newbie in programming..just want to ask how will I compare the contents of particular cell of the table(from parent window) to the contents of a cell of the table (child window)? how will I...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.