title: Dialog box runs for 1 sec and disappears?
body: <p>I'm running a dialog box upon user leaving the page.  The only thing is it runs for 1 sec and disappears?  I know it has to do with <code>bind('beforeunload')</code>, but the dialog dies sooner than you can read it.</p>
  
  <p>How do I stop this from happening?</p>
  
  <pre><code>$(document).ready(function() {  
  
      // Append dialog pop-up modem to body of page
      $('body').append("&lt;div id='confirmDialog' title='Confirm'&gt;&lt;p&gt;&lt;span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'&gt;&lt;/span&gt;Are you sure you want to leave " + brandName + "? &lt;br /&gt; Your order will not be saved.&lt;/p&gt;&lt;/div&gt;");
  
      // Create Dialog box
      $('#confirmDialog').dialog({
        autoOpen: false,
        modal: true,
        overlay: {
          backgroundColor: '#000',
          opacity: 0.5
        },
        buttons: {
          'I am sure': function() {
            var href = $(this).dialog('option', 'href', this.href);
            window.location.href = href;
          },
          'Complete my order': function() {
            $(this).dialog('close');
          }
        }
      });
  
      // Bind event to Before user leaves page with function parameter e
      $(window).bind('beforeunload', function(e) {    
          // Mozilla takes the
          var e = $('#confirmDialog').dialog('open').dialog('option', 'href', this.href);
          // For IE and Firefox prior to version 4
          if (e){
              $('#confirmDialog').dialog('open').dialog('option', 'href', this.href);
          }
          // For Safari
          e.$('#confirmDialog').dialog('open').dialog('option', 'href', this.href);
      }); 
  
      // unbind function if user clicks a link
      $('a').click(function(event) {
          $(window).unbind();
          //event.preventDefault();
          //$('#confirmDialog').dialog('option', 'href', this.href).dialog('open');
      });
  
      // unbind function if user submits a form
      $('form').submit(function() {
          $(window).unbind();
      });
  });
  </code></pre>