aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/document/hasfocus/index.html
blob: 2cbc9b852436d77fc5540e2476b690b82972737d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
title: document.hasFocus()
slug: Web/API/Document/hasFocus
tags:
  - API
  - DOM
  - Focus
  - Method
  - Reference
  - フォーカス
  - メソッド
translation_of: Web/API/Document/hasFocus
---
<div>{{APIRef}}</div>

<p><code><strong>hasFocus()</strong></code>{{domxref("Document")}} インターフェイスのメソッドで、 {{jsxref("Boolean")}} の値を返し、文書または文書内の何れかの要素がフォーカスを持っているかどうかを示します。このメソッドは、文書内のアクティブな要素がフォーカスを持っているかどうかを特定するために使用することができます。</p>

<div class="note">
<p>文書を見ている時、文書内でフォーカスを持つ要素は常に<a href="/en-US/docs/Web/API/DocumentOrShadowRoot/activeElement">アクティブ要素</a>ですが、アクティブ要素がフォーカスを持っているとは限りません。例えば、フォアグラウンドになっていないポップアップウィンドウ内のアクティブ要素はフォーカスを持ちません。</p>
</div>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox">var <var>focused</var> = <var>document</var>.hasFocus();</pre>

<h3 id="Return_value" name="Return_value">返値</h3>

<p>文書内のアクティブ要素にフォーカスがない場合は <code>false</code> が、文書内のアクティブ要素にフォーカスがある場合は <code>true</code> が返ります。</p>

<h2 id="Example" name="Example"></h2>

<p>この例は、文書がフォーカスを持っているかどうかを300ミリ秒ごとに検査します。 <code>hasFocus()</code> の機能をテストするために、ボタンを押して新しいウィンドウを開き、二つのページの間を切り替えてみてください。</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;p id="log"&gt;Awaiting focus check.&lt;/p&gt;
&lt;button onclick="openWindow()"&gt;Open a new window&lt;/button&gt;</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">function checkPageFocus() {
  let body = document.querySelector('body');
  let log = document.getElementById('log');

  if (document.hasFocus()) {
    log.textContent = 'This document has the focus.';
    body.style.background = '#fff';
  }
  else {
    log.textContent = 'This document does not have the focus.';
    body.style.background = '#ccc';
  }
}

function openWindow() {
  window.open('https://developer.mozilla.org/', 'MDN', 'width=640,height=320,left=150,top=150');
}

// Check page focus every 300 milliseconds
setInterval(checkPageFocus, 300);</pre>

<h3 id="Result" name="Result">結果</h3>

<p>{{EmbedLiveSample("Example")}}</p>

<h2 id="Specification" name="Specification">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
   <th scope="col">状態</th>
   <th scope="col">備考</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'interaction.html#dom-document-hasfocus', 'Document.hasFocus()')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>初回定義</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの対応</h2>

<p>{{Compat("api.Document.hasFocus")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li><a href="/ja/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API">Page Visibility API の使用</a></li>
</ul>