Document.hasFocus()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015ë 7ì.
Document.hasFocus() ë©ìëë 문ì ëë 문ì ë´ì ìì(element) ì¤ ì´ë íëë¼ë í¬ì»¤ì¤(focus)를 ê°ê³ ìì¼ë©´ true, ê·¸ë ì§ ìì¼ë©´ false ì¸ Boolean ê°ì ë°ííë¤. ì´ ë©ìë를 ì¬ì©íì¬ ë¬¸ìë´ íì±íë(active) ììê° í¬ì»¤ì¤ë¥¼ ê°ê³ ìëì§ íë¨í ì ìë¤.
ì°¸ê³ : 문ì를 ë³¼ ë, í¬ì»¤ì¤ë¥¼ ê°ì§ ììë ì¸ì ë 문ììì íì±íë ììì´ë¤. ë°ë©´ì íì±íë ììë ê¼ í¬ì»¤ì¤ë¥¼ ê°ì§ ìì ì ë ìë¤. ì를 ë¤ë©´ ì ë©´ì ëììì§ ìì(not a foreground) íì ì°½ ë´ì íì±íë ììë í¬ì»¤ì¤ë¥¼ ê°ê³ ìì§ ìë¤.
구문
focused = document.hasFocus();
ë°í ê°
문ì ë´ì íì±íë ììê° í¬ì»¤ì¤ë¥¼ ê°ê³ ìì§ ìì¼ë©´ false를 ë°í, í¬ì»¤ì¤ë¥¼ ê°ê³ ìë¤ë©´ true를 ë°í
ìì
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>TEST</title>
<style>
#message {
font-weight: bold;
}
</style>
<script>
setInterval(CheckPageFocus, 200);
function CheckPageFocus() {
var info = document.getElementById("message");
if (document.hasFocus()) {
info.innerHTML = "The document has the focus.";
} else {
info.innerHTML = "The document doesn't have the focus.";
}
}
function OpenWindow() {
window.open(
"http://developer.mozilla.org/",
"mozdev",
(width = 640),
(height = 300),
(left = 150),
(top = 260),
);
}
</script>
</head>
<body>
<h1>JavaScript hasFocus example</h1>
<div id="message">Waiting for user action</div>
<div><button onclick="OpenWindow()">Open a new window</button></div>
</body>
</html>
ëª ì¸
| Specification |
|---|
| HTML > # dom-document-hasfocus-dev > |