...

View Full Version : Pop Up Variables to function in Parent Window



ccurle
12-07-2006, 09:00 PM
I have a form that is a pop up window, and when you fill it out, the variables get sent to a javascript function in the parent window.

I put in the alert box to see if the variables were passing, and they were, and then I discovered that after I click the OK button in the alert box, the script works, if I take it out it doesn't work. Could there be a timing issue or something I am not doing right?

I don't understand why it works with an alert box, but not without.


Here is the code for the pop up

var newCat = null;
function addCat() {
if (newCat && !newCat.closed) newCat.close();
newCat = open('admin_create_category.php','newCat','left=300,top=85,width=350,height=270,status=0');
}

Here is the code in the pop up window

function setOpener(oForm) {
if (opener && !opener.closed) {
var newChanName = oForm.chanName.value;
var newChanDesc = oForm.chanDesc.value;
opener.addCategory('channelResult',newChanName,newChanDesc);
self.close();
return false;
}
}


Here is the function in the parent window that sends to php page

function addCategory(div_id,cn,cd) {
subject_id = div_id;
var channelID = document.program.Channel[document.program.Channel.selectedIndex].value;
var addURL = "admin_create_category_result.php?ChanName=" + escape(cn) + "&ChanDesc=" + escape(cd) + "&ChannelGK=" + escape(channelID);
alert("You have added " + cn + " to the category list");
http.open("GET", addURL, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

Thanks in advance

nikkiH
12-08-2006, 02:53 AM
The first thing I notice is that you're closing, then returning. You can't do both, in either order. If you try to close, that window is done and the return doesn't happen. If you return, you're done, so the close would never happen.
That doesn't really matter, though, because you don't use the return value so there's no real point to having one.

That said, I don't think a call to an opener function is a blocking call in javascript. That is, I don't think it waits for the call to complete before moving on to the next line. You may need to set up a mini-state machine, where it waits for some other condition before moving on (like http request with AJAX, http.onreadystatechange = handleHttpResponse; ).
Or you could cheat and put in a timer, but you'd be taking a chance that it doesn't finish in time. :D

ccurle
12-08-2006, 03:03 AM
This is how the others are setup.

http.open("GET", "admin_delete_category.php?Category=" + escape(content) + "&ChannelGK=" + escape(channelID), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);

so would I change it to this?

http.open("GET", opener.addCategory('channelResult',newChanName,newChanDesc);, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);

I am very, very, very new to this and the code I have been using is one I found online. I am teaching myself along the way.

nikkiH
12-11-2006, 03:48 PM
No, I didn't mean to change that code, I meant that onreadystatechange is a classic example of a state machine.

You have to implement a little state machine of your own for addCategory so that you know when the code has finished. In calling code, you'd wait for that state to occur.
If you're new to coding, you may just want to set a timer for now so it waits to close the window.

In the long run, the code should execute something like this:
(pseudocode-ish, method calls may not yet exist ;) )



function setOpener(oForm) {
if (opener && !opener.closed) {
var newChanName = oForm.chanName.value;
var newChanDesc = oForm.chanDesc.value;

opener.addCategory('channelResult',newChanName,newChanDesc);
while (! opener.isFinished) {waitTimerCall();}

self.close();
return false;
}
}


The parent would then have to have a method that sets isFinished and whatnot. The timer call would just emulate a "sleep 10" or some such.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum