0

The situation is the following, I have a asp page with a button that sends a save request to the server (onClick), before this is done however some things have to be checked and if necessary more information has to be asked to the user by means of a pop up. This is done by running a js function for OnCLientClick, if these validations pass then we proceed to save.

The JS looks as follows, we first check if we need the extra information, if this is the case we open a new page by means of a popup, this popup then calls the opener to set a boolean. Depending on this boolean we want the save of OnClick to continue or not.

The code of the parent js is as follows:

var FilledIn = false;
function f1(url){
    if(validate){
        var popupObject = window.open(url);  
        popupObject.focus();
        return setTimeout(function(){
        if (popupObject.closed){
            return FilledIn; //Global set by the child
        }
        }, 500);
    }
    else  {
      return true;
    }
}

The problem is that the filledIn value is already returned even before the popup is closed, leading to the save already happening (if true), this while this should wait until the pop up is closed (how does not matter). I used the timeout in order to prevent this, but this does not yield the expected results. How can I get the desired effect?

smitske
  • 21
  • 3
  • 2
    Asynchronous coding issue! Look it up, thousands of these on this website. – Mouser Oct 29 '17 at 14:34
  • `f1` **cannot** return the result from the popup, see the linked question's answers for why. Instead, `f1` needs to accept a callback it calls when the result is provided by the child, or a promise it resolves the the result is provided. – T.J. Crowder Oct 29 '17 at 14:36

0 Answers0