const isTouchDevice = ('ontouchstart' in window) ||
(navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0);
console.log(`isTouchDevice: ${isTouchDevice ? 'TRUE' : 'FALSE'} `);
const button = document.getElementById('btnClear');
const divEvents = document.getElementById('divEvents');
const olEvents = document.getElementById('olEvents');
const divBottom = document.getElementById('divBottom');
button.addEventListener('click', function() {
if (isTouchDevice) {
button.focus();
setTimeout(() => button.blur(), 500);
}
olEvents.innerHTML = '';
});
const eventNames = [
'load',
'focus',
'blur',
'change',
'close',
'error',
'haschange',
'message',
'offline',
'online',
'pagehide',
'pageshow',
'popstate',
'resize',
'submit',
'unload',
'beforeunload'
];
eventNames.forEach(function(eventName) {
window.addEventListener(eventName, function(evt) {
const now = new Date();
const timeStr = now.getHours().toString().padStart(2, '0') + ':' +
now.getMinutes().toString().padStart(2, '0') + ':' +
now.getSeconds().toString().padStart(2, '0') + '.' +
now.getMilliseconds();
let li = document.createElement('li');
li.innerHTML = timeStr + ' - ' + `<code>${evt.type}</code>`;
olEvents.appendChild(li);
const bottomOffset = divBottom.offsetTop;
divEvents.scrollTop = bottomOffset - 10;
});
});
#divEvents {
border: 1px solid rgba(0, 0, 0, 0.5);
height: 400px;
max-width: 60rem;
padding: 1rem 0;
overflow-y: auto;
}
#olEvents {
font-size: 87.5%;
}
#divBottom {
height: 0px;
}
code {
font-size: 100%;
}
@media (hover:none) {
.btn-outline-primary:hover {
color: #007bff;
background-color: transparent;
background-image: none;
border-color: #007bff;
}
.btn-outline-primary:focus {
color: #fff;
background-color: #007bff;
border-color: #007bff;
}
}
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, maximum-scale=1, minimum-scale=1, shrink-to-fit=no, user-scalable=no">
<meta name="apple-mobile-web-app-title" content="WinEvents">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>Test of Window Events</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootsdark@latest/dist/bootsdark.min.css">
</head>
<body class="d-flex flex-column h-100">
<header>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="https://terrymorse.com">Terry Morse
Software</a>
</nav>
</header>
<main role="main" class="flex-shrink-0 m-4">
<h1>Test of Window Events</h1>
<p>Displays all of the events (except for
<code>scroll</code>) sent to <code>document.window</code>.</p>
<p>
<button id="btnClear" class="btn btn-sm btn-outline-primary"
>Clear History</button>
</p>
<h4>Events Caught:</h4>
<div id="divEvents">
<ol id="olEvents" class="text-monospace"></ol>
<div id="divBottom"></div>
</div>
</main>
</body>