PDA

View Full Version : Getting info from parent to child?


EdNerd
02-23-2010, 04:50 PM
I posted this in the general JavaScript forum, but wasn't getting any reply, so on the chance I was in the wrong place, I am re-posting the question here.

I'm looking at creating a set of customer contact forms. I'd like to be able to have a main contact page where the visitor fills in general info like name and such, and then clicks a link to get into the specific area form he needs.

I've done VB programming and used it to manipulate an IE document object. I've explored JavaScript and managed to code one or two IF functions. I'm wondering if I open the linked form in a new window, can I use JS to pull the info from the first window and insert it into the new window? Seems like it would be easy, but I'm having some trouble.

I have included only the bits of code that seem to be the problem (since everything else was okay before i started fiddling with it!)

In my parent window, I have

<form name="TopInfo">

and

<input size="30" name="txtConName">


In the child window, I have

<body background="graphics/fiebkgnd.jpg" onload="GetInfo()">

and

function GetInfo(){
varName = window.opener.document.TopInfo.txtConName.value
alert(varName)
}

I'm working in PageBreeze (free). Every time I save, it tells me that window.opener.document is not an object.

When I open the parent page in IE, fill in the text box, and click the link, the child page opens but again I get a JavaScript error.

Is there any help for this?
Ed

glenngv
02-23-2010, 09:37 PM
Did you open the child window from parent using window.open method?

EdNerd
02-23-2010, 11:41 PM
No - it's a standard HREF hyperlink. Do I need to change that for this to work?

Ed

Kor
02-24-2010, 10:09 AM
No - it's a standard HREF hyperlink. Do I need to change that for this to work?

Ed
Definitely. The opener is the reference of a document from which the respective pop-up/new document was opened on using window.open() method.

EdNerd
02-24-2010, 03:34 PM
D'oh!! Makes perfect sense - which is probably why it didn't occur to me!!

Okay - I now have:

<a onclick="javascript:window.open('URL.htm')" href="">Text</a>

in the parent and

function GetInfo(){
varName = window.opener.document.TopInfo.txtConName.value
alert(varName)
}

in the child window.

Clicking the parent's link opens the child in a new window and passes the value into the alert box. WAH-HOO!! Thank you!

Uh, but on opening the child window, the two IE windows go to the background and a folder open on my desktop becomes the active window?? Am I missing a parameter?

Ed

Kor
02-24-2010, 03:57 PM
Uh, but on opening the child window, the two IE windows go to the background and a folder open on my desktop becomes the active window?? Am I missing a parameter?

Ed
A folder? Which folder?

On the other hand, if you are using an alert() called the opener, of course that the pop-up loses its focus.

EdNerd
02-24-2010, 04:38 PM
A folder? Which folder?

On the other hand, if you are using an alert() called the opener, of course that the pop-up loses its focus.

Ah ha! It's the folder that contains the web pages I'm working with! And even if I close it, it opens up and comes to the front. An unrelated folder stays in the background. Can I safely assume that when I put this on the internet vice working with it on my computer that won't happen?

So I also got rid of the alerts. I created a text box

<input name=ClientName>

and adjusted my function to

function GetInfo(){
varName = window.opener.document.TopInfo.txtConName.value
document.GetElementByID(ClientName).value = varName
}


The folder still opens.
The text box is empty?!

Ed

glenngv
02-24-2010, 10:32 PM
<a href="URL.htm" onclick="window.open(this.href); return false;">Text</a>

function GetInfo(){
var clientName = (opener && !opener.closed) ? opener.document.TopInfo.txtConName.value : ""; //check if opener exists and is still open, else set client name to empty
document.getElementById("ClientName").value = clientName;
}

<input name="ClientName" id="ClientName" />

EdNerd
02-24-2010, 11:15 PM
Glenn:

Thank you!! Thank you!! Thank you!! Thank you!!

It took me a few tries because I kept not copying your example exactly. (I'm using PageBreeze free HTML editor and it does very nasty things when you forget a ;!!!)

But finally - it works!!!
Awesome!!

Thanks again!
Ed