在重定向到另一个页面(targetpage.html)之前,我在点击链接后使用swal2在页面(sourcepage.html)上获取确认对话框。这到目前为止运作良好。但是,在被重定向到targetpage.html并希望使用浏览器“back-button”返回到sourcepage.html之后,页面被“变暗”并且不再可用了,这是因为<div class="swal2-container .....>
仍然存在并覆盖页面完全。如果我在该页面上点击“重新加载”,容器就会消失。
这是我目前的代码:
HTML
<a href="#" onclick="JSconfirm()">Redirect me to targetpage.html</a>
JS
function JSconfirm(){
swal.fire({
title: "Redirect",
type: "warning",
html: "Here comes custom text with html",
showCancelButton: true,
closeOnConfirm: true,
closeOnCancel: true,
confirmButtonColor: "#DD6B55",
cancelButtonText: "No",
confirmButtonText: "Yes"
}).then(function (result) {
if (result.value) {
window.location = "targetpage.html";
}
});
}
我该怎么做才能确保这个div不再可见。点击“是”确认重定向到“targetpage.html”后,应该从sourcepage.html“删除”。
非常感谢
我自己找到了解决方案
在重定向之前添加swal.close和超时。
}).then(function (result) {
if (result.value) {
swal.close();
setTimeout (
() => {
window.location.href = "targetpage.html";
},
1 * 500
);
}});
}