title: how to call window onunload event for iFrame button click where parent page and iFrame resides in different domain
body: <p>I have button which opens an iFrame <em>(which reside in domain say 'xyz')</em> the iFrame loads the page which resides in another domain <em>(say 'lmn')</em></p>
  
  <pre><code>$("#templateSelectionFrame").get(0).contentWindow.location.href = url;
  </code></pre>
  
  <p>url is from another domain <em>(I communicate in different domain with Jquery 'POSTMESSAGE')</em></p>
  
  <p>when user clicks cancel button from iFrame i need to close the iFrame but before that it is necessary to show the page leaving message (ie. stay on page / leave page - this is alreday called on <code>window.onbeforeunload</code>).</p>
  
  <pre><code>var warnNavigateAway = true;
  
  window.onbeforeunload = confirmBrowseAway;
  
  // Called then user leave window without saving content.
  function confirmBrowseAway() {
      if (warnNavigateAway) {
          return "If you leave this page, your work will be lost ...";
      }
  }
  
  function documentSaved() {
     warnNavigateAway = false;
  }
  
  function documentDirty() {
     warnNavigateAway = true;
  }
  
  
  &lt;div id="bottomBar"&gt;
     &lt;button class="cancelBtn" onclick="GoBack()"&gt;Cancel&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>how do i call the same page leaving message so that i can close the iFrame when user clicks leave page message.</p>
  
  <pre><code>function GoBack() 
  {
     var operationType = '&lt;%: (Model.OperationType)%&gt;';
     if (operationType == "Create")
     {
        window.history.back();
     }
     else {     
        $.postMessage(
           'closeIFrame',
           parentUrl,
           parent
        );
     }
  }
  </code></pre>
  
  <p>(navigation: cancel button -> page leave message -> if(stay on page then 'nothing to do') if(leave page then ) -> close the iFrame)</p>