Passing a reference to the popup window

D

DIZZIEDAZZ

Is there a way, I could pass the reference to a pop up window to
another function?

Basically, this is what I want to
var win= window.open("x.htm".....)

SetValue(win,"txtName");

//-------------------------------------------
function SetValue (win, ctlID)
{
var obj=win.getElementByID(ctlID);
ctlID.value="Hi";
}


I am getting errors when I do this. Any help is appreciated. Thanks
_GJK
 
T

Thomas 'PointedEars' Lahn

DIZZIEDAZZ said:
Is there a way, I could pass the reference to a pop up window to
another function?

Of course there is.
Basically, this is what I want to
var win= window.open("x.htm".....)

SetValue(win,"txtName");

//-------------------------------------------
function SetValue (win, ctlID)

Consider using a lowercase character for first character of the function
identifier. It helps to tell simple functions and those designed to serve
as constructors apart, and is thus considered a Good Thing regarding code
style.
{
var obj=win.getElementByID(ctlID);
ctlID.value="Hi";

`ctlID' is a string, not the object reference you tried to retrieve.
}


I am getting errors when I do this. [...]

"Does not work" is a useless error description. [psf 4.11]
See <http://jibbering.com/faq/#FAQ4_43>.

getElementById() -- notice the case -- is a method of Document objects,
not of Window objects. Try

var obj = win.document.getElementById(ctlID);
if (obj && typeof obj.value != "undefined")
{
obj.value = "Hi";
}

See also

<http://www.quirksmode.org/js/support.html>
<http://pointedears.de/scripts/test/whatami>

for required feature tests (an example was given above).

However, since you are trying to assign something to the `value' property
in the process, I assume you are trying to modify the value of a form
control. You should not use getElementById() in that case as it requires
W3C DOM Level 2 Support and is probably slower than the alternative.

Consider making it a child element of a `form' element; this would allow
you to refer to the form control by

win.document.forms[...].elements[ctlID]

where `...' would either be the 0-based index or the name string of the
`form' element. Most of this referencing (after document) is specified
in W3C DOM Level 2 HTML, however it is also backwards compliant.


HTH

PointedEars
 
L

Lee

DIZZIEDAZZ said:
Is there a way, I could pass the reference to a pop up window to
another function?

Basically, this is what I want to
var win= window.open("x.htm".....)

SetValue(win,"txtName");

//-------------------------------------------
function SetValue (win, ctlID)
{
var obj=win.getElementByID(ctlID);
ctlID.value="Hi";
}


I am getting errors when I do this. Any help is appreciated. Thanks

What makes you think you're failing to pass the reference to the window?
Could your error be caused by the fact that window Objects don't have
a getElementById() method?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top