我正在尝试做一些类似于你离开的网站,它会显示一个弹出窗口“你确定要离开这个页面”,并有两个选项说“取消”和“确定”。 / p>
当我点击“取消”时,我会如何做到这一点,它只会取消该框,当他们点击“确定”时,它会执行'leaveChat();'功能并离开网站?
我已经尝试使用onbeforeunload工作,但我不确定功能部分。
感谢您的帮助!
答案 0 :(得分:0)
没有办法做到这一点。
您可以尝试在onunload
中发送AJAX请求,但我认为这不会可靠地运行。
答案 1 :(得分:0)
要在用户离开页面确认离开时弹出消息,您只需执行以下操作:
<script>
window.onbeforeunload = function(e) {
return 'Are you sure you want to leave this page? You will lose any unsaved data.';
};
</script>
调用函数:
<script>
window.onbeforeunload = function(e) {
callSomeFunction();
return null;
};
</script>
答案 2 :(得分:0)
这是我的HTML
<!DOCTYPE HMTL>
<meta charset="UTF-8">
<html>
<head>
<title>Home</title>
<script type="text/javascript" src="script.js"></script>
</head>
<body onload="myFunction()">
<h1 id="belong">
Welcome To My Home
</h1>
<p>
<a id="replaceME" onclick="myFunction2(event)" href="https://www.ccis.edu">I am a student at Columbia College of Missouri.</a>
</p>
</body>
所以这就是我在javaScript中做类似的事情
var myGlobalNameHolder ="";
function myFunction(){
var myString = prompt("Enter a name", "Name Goes Here");
myGlobalNameHolder = myString;
if (myString != null) {
document.getElementById("replaceME").innerHTML =
"Hello " + myString + ". Welcome to my site";
document.getElementById("belong").innerHTML =
"A place you belong";
}
}
// create a function to pass our event too
function myFunction2(event) {
// variable to make our event short and sweet
var x=window.onbeforeunload;
// logic to make the confirm and alert boxes
if (confirm("Are you sure you want to leave my page?") == true) {
x = alert("Thank you " + myGlobalNameHolder + " for visiting!");
}
}