简体   繁体   English

无法删除beforeunload事件

[英]Not able to remove the beforeunload event

We are using following code to show default pop up on refresh, tab close events 我们正在使用以下代码来显示刷新,选项卡关闭事件时的默认弹出窗口

var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable

myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
  if ($('#timeExpiredtxt').hasClass('hide') && $("#submittedAnsResponseText").hasClass("hide")) {
    var confirmationMessage = 'You are attempting an assessment. Are you sure you want to leave this assessment?';
    (e || window.event).returnValue = confirmationMessage;
    return confirmationMessage;
  }
});

I want to remove and unbind this event at runtime. 我想在运行时删除并取消绑定此事件。 I tried the following code but no luck. 我尝试了以下代码,但没有运气。

$(window).off("beforeunload"); 
$(window).off("onbeforeunload"); 
window.onbeforeunload = null;

$(window).unbind("beforeunload"); $(window).unbind(“ beforeunload”);

Had to change the event binding code. 不得不更改事件绑定代码。

function closeIt() {
    if ($('#timeExpiredtxt').hasClass('hide') && $("#submittedAnsResponseText").hasClass("hide")) {
        return "Any string value here forces a dialog box to \n" +
             "appear before closing the window.";
    }
}
window.onbeforeunload = closeIt;

and unbind used below code 并取消绑定以下代码

window.onbeforeunload = null; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM