Javascript 如果单击了具有特定id的按钮,则在卸载前禁用JS
问题:Javascript 如果单击了具有特定id的按钮,则在卸载前禁用JS,javascript,button,onbeforeunload,Javascript,Button,Onbeforeunload,问题: window.addEventListener("beforeunload", function (e) { var confirmationMessage = "Not a good idea - want to reconsider?"; (e || window.event).returnValue = confirmationMessage; //Gecko + IE return confirmationMessage; //Gecko + Webki
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Not a good idea - want to reconsider?";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
});
<a href="survey.php" class="btn btn-primary" id="initiatesurvey">Next</a>
卸载前我有一个
警告标志,如果用户试图离开网页,该标志将被激活。如果用户按下特定按钮,我希望禁用此行为
JS:
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Not a good idea - want to reconsider?";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
});
<a href="survey.php" class="btn btn-primary" id="initiatesurvey">Next</a>
HTML:
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Not a good idea - want to reconsider?";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
});
<a href="survey.php" class="btn btn-primary" id="initiatesurvey">Next</a>
所需输出:
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Not a good idea - want to reconsider?";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
});
<a href="survey.php" class="btn btn-primary" id="initiatesurvey">Next</a>
在激活卸载之前,无需单击“下一步”按钮。我已经在SO上看到了很多解决方案,但我正在寻找纯JS的解决方案。当他们按下按钮时,移除事件侦听器。为此,您需要使用命名函数而不是匿名函数,以便在调用
removeEventListener
时可以引用它
卸载前的函数(e){
var确认消息=“不是一个好主意-想重新考虑吗?”;
(e | | window.event).returnValue=confirmationMessage;//Gecko+IE
返回确认消息;//Gecko+Webkit、Safari、Chrome等。
}
window.addEventListener(“beforeunload”,beforeunload);
document.getElementById(“initiatesurvey”).addEventListener(“单击”,函数(){
removeEventListener(“beforeunload”,beforeunload);
});我看不到buttonPushed所指的是什么?很抱歉,这是我以前版本的答案,我忘了删除它。@Barmar您好,我将在ie11中看到一个弹出窗口,上面写着“无法获取未定义或空引用的属性'addEventListener'。Barmar最后一行以
document
开头。