document.addEventListener(
	'visibilitychange',
	() => {
		if (document.hidden) {
			console.log("the document is not visible");
		} else {
			console.log("the document is visible");
		}
	}
);
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.