Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

document.hasFocus

概要

hasFocus 属性は、指定したドキュメント内のどこかにフォーカスが当たっている場合は true を返します。

註: When viewing a document, an element with focus is always the active element in the document, but an active element does not necessarily have focus. For example, an active element within a (popup) window that is not the foreground has no focus.

構文

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 (
    "https://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>

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート Issue 64846 3.0 (1.9) 6.0 未サポート (有)
機能 Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本サポート ? 1.0 (1.9) ? 未サポート ?

仕様書

関連情報

ドキュメントのタグと貢献者

タグ: 
 このページの貢献者: fscholz, AshfaqHossain, ethertank, Kohei
 最終更新者: AshfaqHossain,