title: Kendo UI Window Close Event: prevent window close
body: <p>We are creating a warning message for an app with various forms. In simple pages it's very easy, we just detect changes in form elements and if a user wants to unload the page we show them the warning message.</p>
  
  <p>But... we also have some forms in Kendo Windows, the thing is that we need to show that same confirmation message if a user wants to close the window. This is the script we have now:</p>
  
  <pre><code>$('div:has(div[data-role="window"])').find('a:has(span.k-i-close)').live('click', function (e) {
      if (formHasChanged) {
          alert('pepe');
          return false;
      }
      return true;
  });
  </code></pre>
  
  <p><strong>The problem with that script is that it's not preventing the window from closing</strong>, the close event seems to be happening before our alert.
  This solution may work <a href="http://www.kendoui.com/forums/ui/window/new-event-onclosing.aspx" rel="nofollow noreferrer">http://www.kendoui.com/forums/ui/window/new-event-onclosing.aspx</a>, but our windows are created on the fly.</p>
  
  <p>Does anyone have a clue of how to sort this out?</p>
  
  <p>Thanks in advance!</p>
  
  <p>Code were we wanted to insert this kendo thingy workaround:</p>
  
  <pre><code>var formHasChanged = false;
  $('form.withWarningMessage').find('input,select,textarea').live('change', function () {
      formHasChanged = true;
      window.onbeforeunload = function () {
          if (formHasChanged) {
              return confirmWarningMessage;
          }
      };
      $('input:submit').live('click', function () {
          formHasChanged = false;
      });
  });
  </code></pre>