Page Visibility API
Detect when page is visible or hidden
BeginnerTopic: Browser API
JavaScript Page Visibility API Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
console.log('Page hidden');
} else {
console.log('Page visible');
}
});Output
Page visible Page hidden
Understanding Page Visibility API
Page Visibility API detects tab visibility.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.