Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Pop Up Variables to function in Parent Window

    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
    Code:
    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
    Code:
    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
    Code:
    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

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is how the others are setup.
    Code:
    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?
    Code:
    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.

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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 )

    Code:
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •