使用devexpress自定义确认弹出页面卸载



所以我一直试图在页面卸载前提示确认,但我不想要这个默认的ok取消浏览器确认弹出窗口,所以根据如何做到这一点的示例,我一直试图用Devexpress弹出窗口或确认对话框替换它,但没有成功,我对javascript相当陌生,并没有真正了解问题的原因。

function confirmExit(data){
setTimeout(function (){
var result =  DevExpress.ui.dialog.confirm("Are you sue","Confirm");
result.done(function (retVal){


if (retVal == true) {

return true;
}
else {
window.stop();
return false;
}

});
});
}
$(window).bind('beforeunload',  confirmExit);

正常的confirm和devexpress弹出或确认对话框的区别在于,后者返回promise。。但由于某些原因,上面的代码甚至不能阻止页面卸载。

任何指导和帮助都将不胜感激。

坏消息是,beforeunload事件无法完成您要查找的内容。我在下面给你看一个可行的代码。下面的代码将显示浏览器的默认警报;如果您退出,现有的更改将丢失";或者类似的东西。这是因为只有当您与页面交互,然后想要退出时,才会触发事件。

window.addEventListener("beforeunload", (e) => {
//console.log(e)
let dialogText = "something";
e.returnValue = dialogText;
return dialogText;
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input type="text" />
<a href="./test.html">exit</a>
</body>
</html>

每个浏览器在处理此事件时的工作方式略有不同,但简而言之。如果你最后不回来,那就行不通了。简单地显示自定义消息是不可能的。您可以在此处找到更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event

更新

如果您希望在用户单击链接时显示警告消息,可以执行以下操作。创建一个按钮并将其设置为链接样式。将按钮绑定到将打开自定义模态的单击事件。在模式的确认按钮中,分配一个重定向到目标页面的点击事件。以下是引导程序的示例:

function exit(){
window.location.replace("https://stackoverflow.com/questions/71811083/using-a-devexpress-custom-confirm-popup-on-pageunload/71878906?noredirect=1#comment127031969_71878906")
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>
<title>Hello, world!</title>
</head>
<body>
<!-- Button trigger modal -->
<a
type="button"
class="fs-2"
title="https://stackoverflow.com/questions/71811083/using-a-devexpress-custom-confirm-popup-on-pageunload/71878906?noredirect=1#comment127031969_71878906"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
Go to a third party page
</a>
<!-- Modal -->
<div
class="modal fade"
id="exampleModal"
tabindex="-1"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Personalized Message</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">Are you sure","Confirm</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal"
>
Close
</button>
<button type="button" class="btn btn-primary" onclick="exit()">Confirm and Exit</button>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"
></script>

<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
-->
</body>
</html>

这是一个基本的想法。你可以创建代码,用你网站上的所有链接或一个链接来实现这一点。

最新更新