aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/api/window/getselection/index.html
blob: fa9cfb319073a70e3f1e103977856116aacba988 (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
---
title: Window.getSelection()
slug: Web/API/Window/getSelection
translation_of: Web/API/Window/getSelection
---
<div>{{ ApiRef() }}</div>

<h2 id="Summary" name="Summary">요약</h2>

<p>사용자 또는 Caret의 위치에 따라 선택된 텍스트의 범위를 나타내는 {{domxref("Selection")}} 객체를 반환한다. </p>

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

<pre class="syntaxbox"><em>selection</em> = <em>window</em>.getSelection();</pre>

<ul>
 <li>selection은 {{domxref("Selection")}} 객체입니다. 빈 문자열("")을 추가하거나 {{domxref("Selection.toString()")}}을 호출하면 선택된 text를 반환합니다.</li>
</ul>

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

<pre class="brush:js">function foo() {
    var selObj = window.getSelection();
    alert(selObj);
    var selRange = selObj.getRangeAt(0);
    // do stuff with the range
}</pre>

<h2 id="Notes" name="Notes">노트</h2>

<h3 id="Selection_객체의_문자열_표현">Selection 객체의 문자열 표현</h3>

<p>JavaScript에서 문자열을 인자로 받는 함수({{ Domxref("window.alert()") }}{{ Domxref("document.write()") }} 같은)에 객체를 전달하면 해당 객체의 {{jsxref("Object.toString", "toString()")}} 메소드를 호출하고 그 결과를 호출한 함수로 전달합니다. 이를 통해서 실제로는 속성과 메소드를 갖는 문자열이 아닌 일반적인 객체라 하더라도 문자열을 인자로 받는 함수의 인자로 사용할 때에는 객체를 문자열인 것처럼 전달할 수 있습니다.</p>

<p>위 예제에서 selObj를 {{domxref("window.alert()")}}의 인자로 전달하면 <code>selObj.toString()</code>가 자동적으로 호출됩니다. 하지만, selObj에 대해서 <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length" title="JS/String.length">length</a></code> 나 <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr" title="JS/String.substr">substr</a></code> 같은 JavaScript <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String" title="JS/String">String</a>의 속성이나 메소드를 사용하면 객체에 그러한 속성이나 메소드가 없기 때문에 에러나 예상치 못한 결과가 발생합니다. <code>Selection</code> 객체를 문자열로 사용하려면 다음처럼 직접 <code style="font-style: normal;">toString()</code> 메소드를 호출해야 합니다:</p>

<pre class="brush:js;gutter:false;">var selectedText = selObj.toString();</pre>

<ul>
 <li><code>selObj</code>는 <code>Selection</code> 객체입니다.</li>
 <li><code>selectedText</code>는 문자열 (선택한 문자열)입니다.</li>
</ul>

<h3 id="Related_objects">Related objects</h3>

<p>It's also useful to note that you can call {{domxref("Document.getSelection()")}}, which works identically.</p>

<p>HTML inputs provide simpler helper APIs for working with selection (see {{domxref("HTMLInputElement.setSelectionRange()")}}).</p>

<p>Notice the difference between <em>selection</em> and <em>focus</em>. {{domxref("Document.activeElement")}} returns the focused element.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-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 Editing", "#dom-window-getselection", "Window.getSelection()")}}</td>
   <td>{{Spec2("HTML Editing")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{Compat("api.Window.getSelection")}}</p>

<h2 id="See_also" name="See_also">See also</h2>

<ul>
 <li>{{domxref("Selection")}}</li>
 <li>{{domxref("Range")}}</li>
 <li>{{domxref("Document.getSelection()")}}</li>
 <li>{{domxref("HTMLInputElement.setSelectionRange()")}}</li>
 <li>{{domxref("Document.activeElement")}}, {{domxref("HTMLElement.focus()")}}, and {{domxref("HTMLElement.blur()")}}</li>
</ul>