473,662 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ 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.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();
}

}
}
Jul 23 '05 #1
12 1829
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.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();
count--; // add this line here
}
}
}


HTH
Ivo
Jul 23 '05 #2
Ivo wrote:
"Ole Noerskov" wrote
function removeKeywords( )
{
var form_obj = window.opener.d ocument.getElem entById('myform ');
var num_of_elem = form_obj.childr en.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.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.
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_el em;count++)
for (var count = 0, num_of_elem = form_obj.childN odes.length;
count < num_of_elem;
count++)

instead.
{
var name = form_obj.childr en[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.childN odes,
num_of_elem = form_obj.childN odes.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.childr en[count].removeNode();


count--; // add this line here


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>
Jul 23 '05 #3
Thomas 'PointedEars' Lahn wrote:
Ivo wrote:
"Ole Noerskov" wrote <snip>
{
var name = form_obj.childr en[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.childr en[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*********@nu rfuerspam.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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #8
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@nu rfuerspam.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*********@nu rfuerspam.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

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

Similar topics

8
15106
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 to achieve the above which does not work: <script language="jscript"> function PageRedirection() { if(!opener)
1
16021
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" on the pop-up's form I want the pop-up to close & a new page to load in the "parent" window/page. I have this working in IE but cannot get it to work in Firefox. The parent window correctly loads the new page after submitting from the pop-up,...
2
2300
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 like this: childwin = window.open(...) if (childwin == null) childwin.opener = self;
2
1798
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 frameset in the opener window. I started with something like this (what happens when the considions are met already works):
19
31043
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(). The problem is that after the reload, it brings you right to the top of the page. When I click 'refresh" on the original page, it brings me back to the original viewing position. Is there a way to duplicate this in from the popup window. Also,...
5
2854
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 the result to main page (I mean, update the parent page). My popup form looks like this: <form name="form1" action="run.php" method="get" target="???">
4
1914
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 javascript) with a datagrid of these partial match records (by doing a sqlcommand using some query string values taken from opener data entered) *** }
5
7525
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 click the attach it should upload file and close the window and send back control to Main page.. But it is not happening.. CODE: ========= <script LANGUAGE="JavaScript"> if ((window.opener != null) && (! window.opener.closed)) {
1
2292
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 use ID for this? I need to compare because If the two contents are the same, no need to add the table (from the child window) to the table on the parent window.. this is my code Child window note: cell3 contents will be the basis of...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8762
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7365
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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 we have to send another system

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.