Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Communicating with popup windows.

Status
Not open for further replies.

Markh51

Programmer
May 20, 2003
81
GB
How do I "communicate" with a popup window which was opened by another one of my forms ?

The reason why I ask is that a user complete's a form then hit's submit, on this form a popup window is launched and then it is self submitted, to another form. The only problem I am having is having the last form "write" to the popup window (is this because this form did not open it ??)

Any ideas on how to get arounf this problem ??

Cheers,
mark
 
var w = window.open(etc...

w.document.forms[0].inputName.value =

will target the popup.
 
Hmm, I'm not to sure if you know what I mean.

I'm trying to write to the popup using:

w.document.open();
w.document.write(statusTxt);
w.document.close();

There is no FORM inside the popup.

Regards,
Mark
 
in the popup put-

<body onload = &quot;opener.sendText()&quot;>

In the page that opens the popup-

function sendText(){

var statusTxt = &quot;blah&quot;;

w.document.open() etc.
 
yeah, I see what your saying, but the page that opened the popup is no longer available, I am trying to write to the popup from a different page, can this be done ??
 
Only if you pass the window handle to the new page. If you close down the window that opened the popup, without transferring the window handle to a different parent, your popup will be 'lost' for communication purposes.
 
I meant, can you be more specific ?

I am relativley new to JS, so I need quite a bit of help. So anything is appreciated.

Regards,
Mark
 
OK... Windows & JS 101:

When you open a window using the window.open() method, it returns a 'handle' or a pointer to that window.eg
Code:
var myhandle = window.open('mypopup.html');

Now say we have Page1, which opens Page2, Which in turn opens Page 3. We have a chain of window handles Page1 has a handle on Page2. Page2 has a handle on Page3.

If you were to close Page2, the chain would be broken. Therefore, If we want Page1 to be able to write to, close or do anything else to Page3, we must pass back that handle from Page2 to Page1 prior to Page2 closing. We can do this using the onunload method... or even immediately after we create Page3. The code for doing this is quite simple, since Page2 has an inbuilt handle on Page1 called window.opener.

Say we have the following code in Page2 for opening Page3:
Code:
var handleP3 = window.open('page3.html');
We can then add the following directly underneath to pass the reference back to Page1:
Code:
window.opener.Page3Handle = handleP3;
Once this is done, any script in Page1 can refer to Page3Handle in the same manner as if it had been declared within the page.
 
I tried what you said but I just keep getting a JS error saying 'waitWindow' is undefined. Here is the code I am using:

This is what open's the window:

var waitWindow = window.open('popup.cfm');
window.opener.waitWindow = waitWindow;

Then on a completly different page, this is what tries and writes to the window:

if (waitWindow && !waitWindow.closed)
{
var statusTxt ='SOME TEXT';
waitWindow.document.open();
waitWindow.document.write(statusTxt);
waitWindow.document.close();
}

I get the error when I try and write to the window, what am I doing wrong ??

Thanks for your help,
Mark.
 
>> Then on a completly different page, this is what tries and writes to the window

You answered your own question. The window.opener.waitWindow = waitWindow; defines an object under the window object of the page which opened it.

You can't then use that variable from just any page.
 
For any page to use a variable, you would need to use a cookie, however... cookies can only store strings, not objects, and a window handle is an object. So, for a window to use a handle created in a different window, you must pass the object to that page using the code above.
 
You've total lost me here, as I said I a relativley new to programming in JS, so you'll have to give me a better hint than that.

The window in question which I am trying to write to is opened in the same window which is trying to write to it, it just in a different form.

So, if you can help in a little bit more detail that would be great.

Cheers,
Mark
 
OK, What you're asking is probably blisteringly clear, but I'm not quite with you... if you could explain the following statement:
The window in question which I am trying to write to is opened in the same window which is trying to write to it, it just in a different form.
In a little more detail - even better, post some more of the code you're working with - it would be a lot easier for me to give you an accurate answer.

For example, you say the window you are trying to write to is the same window as the one trying to write to it? Do you mean in a separate frame? Two separate documents can't sit in the same window at the same time - does window2 overwrite window1 - do you then need to get some information that was in window1 into window2?

Any clarification you can give me would be appreciated.
 
I'm sorry if this is not clear to you. I will try again:

I have a app written in ColdFusion, which has multiple forms and Templates on ONE page.

This app has different steps as the user completes the form.

On step 3 it asks the user to upload some files, if the user DOES upload files as they click on continue to go to step 4, a POPUP window is spawned. Now when they are on step 4 the code checks to see if the files were uploaded (or otherwise) and then changes the POPUP window (waitWindow) with the appropiate content using:

w.document.open();
w.document.write(statusTxt);
w.document.close();

Now the problem is, when the above text is been executed I get an error saying &quot;'waitWindow' is undefined&quot;, this is probably as it was opened in a different form, but I don't know.

I hope this is clear to you now.

Many Thanks for your time and effort on this problem.
Mark
 
>> as they click on continue to go to step 4,

I'm getting closer to understanding your predicament. In your browser's Address bar, does the URL change (at all) when going from step 3 to step 4... if so you lose the window handle. Let me know if this is the case and we'll work out a strategy to get around this.

Otherwise... here are a couple of errors:
w.document.open();
w.document.close();

open() and close() are not member functions of the document object. They are member functions of the window object. The correct syntax would be

//This assigns a window handle to the new window to the variable 'w'
var w = window.open()

//This closes the window referenced by the window handle stored in the variable 'w'
w.close()

 
no , the URL stays the same, as I use the POST method in order to stop a potiential hacker &quot;seeing&quot; whats going on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top