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

Revision 911939 of Document.hasFocus()

  • 리비전 슬러그: Web/API/Document/hasFocus
  • 리비전 제목: Document.hasFocus()
  • 리비전 아이디: 911939
  • 제작일시:
  • 만든이: PineMt
  • 현재 리비전인가요?
  • 댓글
태그: 

리비전 내용

{{ ApiRef("DOM") }}

Document.hasFocus() 메소드는 문서 또는 문서 내의 요소(element) 중 어느 하나라도 포커스(focus)를 갖고 있으면 true, 그렇지 않으면 false인 {{jsxref("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 (
    "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>

명세

Specification Status Comment
{{SpecName('HTML WHATWG', 'interaction.html#dom-document-hasfocus', 'Document.hasFocus()')}} {{Spec2('HTML WHATWG')}} Initial definition

브라우저 호환성

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 30 {{ CompatGeckoDesktop("1.9") }} 6.0 {{ CompatNo() }} {{ CompatVersionUnknown() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatGeckoMobile("1.9") }} {{ CompatUnknown() }} {{ CompatNo() }} {{ CompatUnknown() }}

관련 참고

리비전 소스

<p>{{ ApiRef("DOM") }}</p>

<p><code><strong>Document.hasFocus()</strong></code>&nbsp;메소드는 문서 또는 문서 내의 요소(element) 중 어느 하나라도 포커스(focus)를 갖고 있으면 <code>true, </code>그렇지 않으면<code> false</code>인&nbsp;{{jsxref("Boolean")}} 값을 반환한다. 이 메소드를 사용하여 문서내 활성화된(active) 요소가 포커스를 갖고 있는지&nbsp;판단할 수 있다.</p>

<div class="note">
<p>문서를 볼 때, 포커스를 가진 요소는 언제나 문서상의 활성화된 요소이다. 반면에 활성화된 요소는 꼭 포커스를 갖지 않을 수 도 있다. 예를 들면 전면에 나와있지 않은(not a foreground) 팝업창 내의 활성화된 요소는&nbsp;포커스를 갖고 있지 않다.</p>
</div>

<h2 id="Syntax" name="Syntax">문법</h2>

<pre>
focused = document.hasFocus();</pre>

<h3 id="반환값">반환값</h3>

<p>문서 내의 활성화된 요소가 포커스를 갖고 있지 않으면 <code>false</code>를 반환, 포커스를 갖고 있다면 <code>true</code>를 반환</p>

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

<pre data-line="17" data-number="">
<code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8" /&gt;
&lt;title&gt;TEST&lt;/title&gt;
&lt;style&gt;
#message { font-weight: bold; }
&lt;/style&gt;

&lt;script&gt;

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
  );
}

&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;h1&gt;JavaScript hasFocus example&lt;/h1&gt;
  &lt;div id="message"&gt;Waiting for user action&lt;/div&gt;
  &lt;div&gt;&lt;button onclick="OpenWindow()"&gt;Open a new window&lt;/button&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<h2 id="Specification" name="Specification">명세</h2>

<table>
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'interaction.html#dom-document-hasfocus', 'Document.hasFocus()')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="브라우저_호환성">브라우저 호환성</h2>

<p>{{ CompatibilityTable() }}</p>

<table>
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>30</td>
   <td>{{ CompatGeckoDesktop("1.9") }}</td>
   <td>6.0</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
  </tr>
 </tbody>
</table>

<table>
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatGeckoMobile("1.9") }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>

<h2 id="관련_참고">관련 참고</h2>

<ul>
 <li><a href="https://developer.mozilla.org/ko/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API">페이지 가시성 API 사용하기</a></li>
</ul>
현재 리비전 복원