document.hasFocus() example
RUN
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> #result{ color: red; font-size: 20px; font-weight: bold; } </style> </head> <body> <h1>Học lập trình miễn phí tại freetuts.net</h1> <p>Click vào bất cứ phần tử nào để focus trang</p> <form action="#" method="POST"> Username: <input type="text" name="fname" placeholder="Nhập Username"><br> Email: <input type="text" name="lname" placeholder="Nhập Email"> </form> <p id="result"></p> <button onclick="myFunction()">Xem kết quả</button> <script> setInterval("myFunction()", 5); function myFunction(){ var x = document.getElementById("result"); if (document.hasFocus()) { x.innerHTML = "Trang đã được focus."; } else { x.innerHTML = "Trang chưa được focus."; } } </script> </body> </html>
PHÓNG TO