嗨识别用户卸载页面时的操作,
因为我认为当用户clicks out of document then mouse position is 0
并且我们可以告诉用户点击关闭按钮" beforeUnload"被称为。
所以我的问题是确定4件事,
当执行任何此操作时" BeforeUnload"叫做。
这是我的代码,
<html>
<head>
<title>Page Title</title>
<script>
var mouse = {x: 0, y: 0};
document.addEventListener('mousemove', function(e){
mouse.x = e.clientX || e.pageX;
mouse.y = e.clientY || e.pageY
}, false);
if (window.addEventListener) { // all browsers except IE before version 9
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Hi";
alert(mouse.x + ' : ' + mouse.y);
return confirmationMessage;
});
}
else {
if (window.attachEvent) { // IE before version 9
window.attachEvent("beforeunload", function (e) {
var confirmationMessage = "Hi";
alert(mouse.x + ' : ' + mouse.y);
return confirmationMessage;
});
}
}
</script>
</head>
<body>
<h1>This is a Heading</h1>
<p id = "myAns">This is a paragraph.</p>
</body>
</html>
&#13;
谢谢你的到来。