title: Return a value from window.open
body: <p>We recently discovered that Chrome no longer supports window.showModalDialog which is problematic because our enterprise application uses this method.</p>
  
  <p>There is, apparently, a short term workaround that allows you to restore showModalDialog but it involves modifying the registry which is too complicated (and risky) four our average user. Therefore I'm not a big fan of this workaround.</p>
  
  <p>The long term solution is obviously to remove all calls to this obsolete method and replace them with a convenient jQuery plugin (such as VistaPrint's <a href="http://vistaprint.github.io/SkinnyJS/jquery.modalDialog.html">Skinny Modal Dialog plugin</a>, for example. Other suggestions are welcome by the way).</p>
  
  <p>The typical scenario we use the modal dialog is to ask the user for Yes/No confirmation before executing an action that cannot be undone, ask the user to agree to terms and condition before proceeding, etc. Typically the onclick event on the "Yes" or "Ok" button in the modal dialog looks like this:</p>
  
  <pre><code>window.returnValue = true;
  window.close();
  </code></pre>
  
  <p>Similarly, the "Cancel" or "No" button looks like this:</p>
  
  <pre><code>window.returnValue = false;
  window.close();
  </code></pre>
  
  <p>The fact that we can return a value from the dialog is very convenient because it allows the "parent" window to be notified whether the user has clicked the "Ok" or the "Cancel" button like so:</p>
  
  <pre><code>var options = "center:1;status:1;menubar:0;toolbar:0;dialogWidth:875px;dialogHeight:650px";
  var termsOfServiceAccepted = window.showModalDialog(myUrl, null, options);
  if (termsOfServiceAccepted) {
      ... proceed ...
  }
  </code></pre>
  
  <p>The last thing I'm going to mention about the showModalDialog is that it works great even when the document displayed in the dialog is from a different domain. It's very common for us to have our javascript running from <a href="http://the-client.com">http://the-client.com</a> but the "Terms of Service" web page is from <a href="http://the-enterprise-vendor.com">http://the-enterprise-vendor.com</a></p>
  
  <p>I need a temporary solution that I can deploy ASAP while we work on the long term solution. Here are my criteria:</p>
  
  <ul>
  <li>minimal code change in existing JavaScript</li>
  <li>the pop up window must be able to return a value to the "parent". Typically this value is a Boolean but it could be any simple type (e.g.: string, int, etc.)</li>
  <li>solution must work even if the URL of the content is from different domain</li>
  </ul>
  
  <p>Here's what I have so far:</p>
  
  <p>1) Add the following method in my JavaScript:</p>
  
  <pre><code>function OpenDialog(url, width, height, callback)
  {
      var win = window.open(url, "MyDialog", width, height, "menubar=0,toolbar=0");
      var timer = setInterval(function ()
      {
          if (win.closed)
          {
              clearInterval(timer);
              var returnValue = win.returnValue;
              callback(returnValue);
          }
      }, 500);
  }
  </code></pre>
  
  <p>As you can see in this method, I try to make the pop up window look as similar to a dialog as possible by hiding the menu and the toolbar, I setup a time every 500 milliseconds to check if the window has been closed by the user and if so, get the 'returnValue' and invoke a callback.</p>
  
  <p>2) replace all calls to showModalDialog with the following:</p>
  
  <pre><code>OpenDialog(myUrl, 875, 650, function (termsOfServiceAccepted)
  {
      if (termsOfServiceAccepted)
      {
          ... proceed ....
      }
  });
  </code></pre>
  
  <p>The fourth parameter to the method is the callback where I check if the user has clicked the "Ok" button before allowing her to proceed.</p>
  
  <p>I know it's a long question but basically it boils down to:</p>
  
  <ol>
  <li>What do you think of the solution I propose? </li>
  <li>In particular, do you think I'll be able to get a returnValue from a window that was opened with window.open?</li>
  <li>Any other alternative you can suggest?</li>
  </ol>