Hàm document.hasFocus() trong Javascript
Phương thức hasFocus()
sẽ kiểm tra xem liệu trang hoặc bất cứ phần tử nào trên trang có được focus hay không. Nếu có bất cứ phần tử nào trên trang được focus, phương thức sẽ trả về True, ngược lại phương thức sẽ trả về false.

Cú pháp
Cú pháp: document.hasFocus()
Cách sử dụng
Kiểm tra trang có được focus hay không:
<!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>
Tham khảo: w3schools.com
Bài viết này được đăng tại [free tuts .net]