I want to make a confirmation before user leaving the page.
(我想在用户离开页面之前做出确认。)
If he says ok then it would redirect to new page or cancel to leave.(如果他说好,那么它将重定向到新页面或取消离开。)
I tried to make it with onunload(我尝试使用onunload来实现它)
<script type="text/javascript">
function con() {
var answer = confirm("do you want to check our other products")
if (answer){
alert("bye");
}
else{
window.location = "http://www.example.com";
}
}
</script>
</head>
<body onunload="con();">
<h1 style="text-align:center">main page</h1>
</body>
</html>
But it confirm after page already closed?
(但它确认页面已经关闭后?)
How to do it properly?(怎么做得好?)
It would be even better if someone shows how to do it with jQuery?
(如果有人用jQuery演示如何做到这一点会更好吗?)
ask by kd7 translate from so