In this video, we’ll learn how to check if the user is currently on a webpage using JavaScript. We’ll cover two different methods for doing this: window.onfocus and window.onblur events. We’ll also discuss the pros and cons of each method and how to use them in your code. By the end of this video, you’ll have a better understanding of how to detect when the user is viewing your webpage, and you’ll be able to use this information to create more engaging and interactive web experiences.
I have posted many videos and articles before related to the JavaScript project, now this the something new that we are going to build.
Video Tutorial of How to check the user is on the webpage or not
As you have seen on the given video tutorial of How to play m3u8 file in html video player using JavaScript, If you are feeling bored watching the given video tutorial of How to play m3u8 file in html video player using JavaScript then you can copy or download the given codes below:
How to check the user is on the webpage or not using JavaScript | Free Source Code
To create this program (To check the user is on the webpage or not). First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste the following codes in your file.
In the first place, make a HTML document with the name of index.html and paste the given codes in your HTML record. Keep in mind, you’ve to make a document with .html extension.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Check the user is on the wabpage or not</title> <style> *{ margin: 0; padding: 0; box-sizing: border-box; font-family: sans-serif; } body { background: #222; display: flex; justify-content: center; align-items: center; min-height: 100vh; } #video{ width: 600px; height: 100%; outline: none; border: none; border-radius: 10px; } </style> </head> <body> <video src="./video" id="video"></video> <script> const video = document.querySelector("#video") window.addEventListener("focus",()=>{ console.log("The user is currently on the webpage") video.play(); }) window.addEventListener("blur",()=>{ console.log("The user is not currently on the webpage") video.pause(); }) </script> </body> </html>
That’s all, now you’ve successfully created a program to check the user is on the webpage or not using JavaScript. If your code doesn’t work or you’ve faced any error/problem, please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.
Click on the following download button to download all source code files.