【发布时间】:2020-02-25 14:41:33
【问题描述】:
关闭浏览器选项卡时有什么方法可以显示弹出窗口吗?在弹出窗口中应该有一个按钮可以查看更多内容,当用户单击该按钮时,用户应该被重定向到查看更多页面。如果用户单击弹出窗口中的取消按钮,用户将离开网站。有没有办法做到这一点?我目前正在尝试此代码。它显示了弹出窗口,但它突然消失了。我认为条件也不起作用。
var userclicked=false;
window.onbeforeunload = function(e) {
//e.preventDefault();
if(!document.hasfocus()){
Swal.fire({
title: 'New Promotions!',
text: 'Visit to See New Promotions!',
type: 'info',
showCancelButton: true,
confirmButtonText: 'View More',
cancelButtonText: 'Cancel',
showCloseButton: true
}).then((result) => {
if (result.value) { userclicked=true;
window.location = "{{url('front/promotionpage')}}";
} else if (result.dismiss === Swal.DismissReason.cancel) { userclicked=true;
}
});
if(userclicked){
return "Hi";
}else{
return null;
}
}
//return "hi";
//window.onbeforeunload = null;
//return null;
//return "Hi";
}
【问题讨论】:
-
不,这是不可能的。在
onbeforeunload中可以做的就是显示一个弹出窗口以确认退出页面(您无法更改其文本)。
标签: javascript jquery laravel