Skip to content

Instantly share code, notes, and snippets.

@tyua07
Created January 16, 2024 19:51
Show Gist options
  • Save tyua07/dbc7ea76ca4eb6f8be43546a41a045c2 to your computer and use it in GitHub Desktop.
Save tyua07/dbc7ea76ca4eb6f8be43546a41a045c2 to your computer and use it in GitHub Desktop.
onbeforeunload
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>onbeforeunload</title>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0,maximum-scale=1.0, user-scalable=0"/>
</head>
<body>
<script>
!function () {
var uuid;
function getUuid() {
console.log("get uuid")
}
function isLoadingSuccess() {
return "complete" === document.readyState || "loading" !== document.readyState && !document.documentElement.doScroll;
}
function loadHandle(callback) {
isLoadingSuccess() ?
window.setTimeout(callback)
:
document.addEventListener
? window.addEventListener("load", callback)
: window.attachEvent("onload", callback)
}
function visitHandle() {
var log_onbeforeunload_timer;
function send_log_data() {
console.log(document.activeElement && document.activeElement.tagName)
if (document.activeElement && "A" === document.activeElement.tagName) {
console.log(document.activeElement , document.activeElement.tagName)
getUuid()
document.activeElement.blur()
if ("IFRAME" === document.activeElement.tagName) {
document.body.focus()
}
if ("IFRAME" === document.activeElement.tagName) {
clearInterval(log_onbeforeunload_timer)
}
return void 0;
}
return !1;
}
log_onbeforeunload_timer = setInterval(function () {
send_log_data()
}, 300)
window.onbeforeunload = function () {
clearInterval(log_onbeforeunload_timer), send_log_data()
}
}
loadHandle(visitHandle)
}()
</script>
<a target="_blank" href="https://so.com" style="display: block;width: 100px;height: 100px;background: red;">so.com</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment