我在用戶離開頁面時正在運行一個對話框。唯一的是它運行1秒並消失?我知道它與bind('beforeunload')
有關,但對話框會比你讀的更快。對話框運行1秒並消失?
如何阻止這種情況發生?
$(document).ready(function() {
// Append dialog pop-up modem to body of page
$('body').append("<div id='confirmDialog' title='Confirm'><p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>Are you sure you want to leave " + brandName + "? <br /> Your order will not be saved.</p></div>");
// 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();
});
});
你需要返回;否則返回FALSE,事件持續了'unload',因此將關閉該窗口.. – balexandre 2011-05-19 19:22:41
返回false,大多數瀏覽器打開一個默認對話框:「停留」或「離開」 – benwasd 2011-05-19 19:31:58
@balexandre:從'beforeunload'返回'false'將會產生一個帶有「false」的警告框。 – 2011-05-19 21:38:10