Javascript 当用户点击web浏览器窗口中的关闭按钮时,jquery确认框
当用户点击Web浏览器的关闭按钮或离开页面或重新加载页面时,是否可以使用jquery模式对话框实现与Javascript window.confirm框相同的功能Javascript 当用户点击web浏览器窗口中的关闭按钮时,jquery确认框,javascript,c#,asp.net-mvc,Javascript,C#,Asp.net Mvc,当用户点击Web浏览器的关闭按钮或离开页面或重新加载页面时,是否可以使用jquery模式对话框实现与Javascript window.confirm框相同的功能 //////javascript code////// sessionStorage["isTrue"] = false; $(".PassReqNotes,.datePassed1").change(function () { sessionStorage["isTrue"]= true; }); $(window).b
//////javascript code//////
sessionStorage["isTrue"] = false;
$(".PassReqNotes,.datePassed1").change(function () {
sessionStorage["isTrue"]= true;
});
$(window).bind('beforeunload', function (event) {
event.preventDefault();
var sessionValue = sessionStorage["isTrue"].toLocaleString();
if (sessionValue == "true") {
isTrue = false;
sessionStorage["isTrue"] = null;
//return 'If you leave page, You may lose some changes';
return window.confirm = dialog();
}
});
function dialog(event) {
$("#dialog-confirmPassReq").removeClass('hide').dialog({
resizable: false,
modal: true,
title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-exclamation-triangle red'></i> Delete Photo?</h4></div>",
title_html: true,
buttons: [
{
html: "<i class='ace-icon fa fa-trash-o bigger-110'></i> ok",
"class": "btn btn-danger btn-xs btnDeleteActivityPhoto",
click: function () {
$(this).dialog("close");
return true;
}
},
{
html: "<i class='ace-icon fa fa-times bigger-110'></i> Cancel",
"class": "btn btn-xs",
"id": "btnClosePassReq",
click: function () {
$(this).dialog("close");
return false;
}
}
]
});
}
是的,您可以将window.onbeforeunload和event.preventDefault结合使用。是的,我尝试了window.onbeforeunload和event.preventDefault,但它不起作用,当用户请求其他页面时,它不请求确认并在新页面上重定向是的,我尝试了window.onbeforeunload,它显示Jquery确认框,但不等待请求用户确认并重新加载页面。
window.onbeforeunload = function() {
//your code
};