Close opened pop-up window on form submission and trigger click on the parent window
Asked Answered
C

3

5

I have a parent window from where I am opening a pop-up window using window.open() and I am monitoring the return of that pop-up window using the window.opener method. I have two queries here:

  1. On my pop-up window, there is a submit button that takes care of the submit functionality of the form and I would like to wait for the submit to complete and close this pop-up window on submission.
  2. When the form is submitted and the pop-up window is closed, I am fetching one of the text box values and using that in my parent window to simulate a search automatically as soon as the pop-up window is closed.

Code:

// parent window handling of information returned from the pop-up window
function HandlePopupResult(value) {
    alert("The field value is: " + value);
    $("input[value='Search']").trigger("click");
}

//pop-up window handling of information to be returned to the parent window on submission
$("#myForm").submit(function() {
    window.opener.HandlePopupResult($("#field").val());
    window.close();
});

So my question is: How can I make sure that the submit is completed in order for me to trigger a search click in my parent window and close this pop-up window? Kindly let me know.

I am able to finally send information back to my parent form properly but I just need to make sure that my submit gets completed. Any suggestions on making sure that I fire my events only after the submit is successful?

Cheers.

Calx answered 21/5, 2015 at 8:58 Comment(3)
Was any of the options at #11535190 valid for you?Deemphasize
Not really. I think I'm about to get to an answer. I'll post it.Calx
Sorry I didn't get back to you sooner on what you asked me. It was a holiday weekend where I live, so I haven't been around until today. I posted some additional information below for you, hope it helps out.Locative
C
0

This would be helpful for others in the future. I was able to achieve this myself by just modifying the above code to the code given below:

// parent window handling of information returned from the pop-up window
function HandlePopupResult(value) {
    alert("The field value is: " + value);
    $("input[value='Search']").trigger("click");
}

Changed the form submission to an AJAX post call to ensure that the form submission has occurred and then carry out my remaining tasks of Handling the result of the pop-up window after which I close the popup window. Works like a charm!

//pop-up window handling of information to be returned to the parent window on submission
$("#myForm").submit(function() {
        $.ajax({
          type: 'POST',
          url: $(this).attr('action'),
          data: $(this).serialize(),
          success: function(data) {
            window.opener.HandlePopupResult($("#field").val());
            window.close();
          }
        });
        return false;
      });
Calx answered 26/5, 2015 at 10:18 Comment(0)
S
0

The new window (popup) you have opened will load a new HTML page on submit (the function of the jQuery submit call is executed before the form is sent to the sever).

You shall do the actions within the new page being loaded in the popup window.

Seifert answered 21/5, 2015 at 9:10 Comment(2)
Is there no way that would tell me that the submit event is completed and I can carry out my other tasks in that form itself?Calx
I did a small research and the most complete explanations on the situation you describe are found in this question: #11535190 . But note that there are plenty of comments as "didn't work for me" for each workaround, so the alternatives shown there (other than the one in my answer) look unreliable.Deemphasize
C
0

This would be helpful for others in the future. I was able to achieve this myself by just modifying the above code to the code given below:

// parent window handling of information returned from the pop-up window
function HandlePopupResult(value) {
    alert("The field value is: " + value);
    $("input[value='Search']").trigger("click");
}

Changed the form submission to an AJAX post call to ensure that the form submission has occurred and then carry out my remaining tasks of Handling the result of the pop-up window after which I close the popup window. Works like a charm!

//pop-up window handling of information to be returned to the parent window on submission
$("#myForm").submit(function() {
        $.ajax({
          type: 'POST',
          url: $(this).attr('action'),
          data: $(this).serialize(),
          success: function(data) {
            window.opener.HandlePopupResult($("#field").val());
            window.close();
          }
        });
        return false;
      });
Calx answered 26/5, 2015 at 10:18 Comment(0)
L
0

Another way to simplify that some would be using the $.post method instead. Saves a few lines, but achieves the same thing.

$.post( url, data, callback);

$.post( $( "#myForm" ).attr('action'), $( "#myForm" ).serialize(), function(){
        window.opener.HandlePopupResult($("#field").val());
        window.close();
});

You can either throw that inside of your .submit as a shorthand replacement for the AJAX request, or throw it into its own function that you call from your form.

<form onsubmit="submitHandler(this); return false" name="myForm" id="myForm">

You can also combine it with JSON to add some further functionality/validation before you have it sending things back to your main page, which could be helpful in many situations. As well, it's an awesome way of returning data from your PHP (or whatever you use) scripts back to your JS/jQuery.

$.post( $( "#myForm" ).attr('action'), $( "#myForm" ).serialize(), function(data){
      if(data.status == 'failed')
      {
        // Lets present an error, or whatever we want
      }
      else
      {
       // Success! Continue as we were
        window.opener.HandlePopupResult($("#field").val());
        window.close();
      }
}, 'json');
Locative answered 26/5, 2015 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.