我想在关闭我的页面时询问用户。我试过这个脚本:
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "alert";
}
</script>
<script type="text/javascript">
var areYouReallySure = false;
function areYouSure() {
if(allowPrompt){
if (!areYouReallySure && true) {
areYouReallySure = true;
var confMessage = "***************************************\n\n W A I T !!! \n\nBefore leaving our site, follow CodexWorld for getting regular updates on Programming and Web Development.\n\n\nCLICK THE *CANCEL* BUTTON RIGHT NOW\n\n***************************************";
return confMessage;
}
} else {
allowPrompt = true;
}
}
var allowPrompt = true;
window.onbeforeunload = areYouSure;
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(window).on('beforeunload', function() {
return "You should keep this page open.";
});
</script>
我读到在现代浏览器中脚本可能无效。有没有其他方法可以解决这个问题?