"JavaScript 클립보드로 복사하기"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 12개는 보이지 않습니다)
5번째 줄: 5번째 줄:
==예시 1: textarea 임시 생성하여 복사 ★==
==예시 1: textarea 임시 생성하여 복사 ★==
{{참고|순수 자바스크립트 클립보드 복사 구현}}
{{참고|순수 자바스크립트 클립보드 복사 구현}}
<source lang='html'>
<syntaxhighlight lang='html' run outheight='80'>
<button id='btn1'>Greet</button>
<button onclick="copy()">COPY</button>


<script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
<script>
function copyToClipboard(val) {
function copyToClipboard(val) {
   var t = document.createElement("textarea");
   const t = document.createElement("textarea");
   document.body.appendChild(t);
   document.body.appendChild(t);
   t.value = val;
   t.value = val;
18번째 줄: 17번째 줄:
   document.body.removeChild(t);
   document.body.removeChild(t);
}
}
$('#btn1').click(function() {
function copy() {
   copyToClipboard('Hello World');
   copyToClipboard('Hello World');
   alert('Copied!');
   console.log('Copied!');
});
}
</script>
</script>
</source>
</syntaxhighlight>
<jsfiddle height='120'>oc41ah8b</jsfiddle>


==예시 2: input에서 복사==
==예시 2: input에서 복사==
* IE 10+, Chrome 43+, Opera 29+, Firefox 가능
* IE 10+, Chrome 43+, Opera 29+, Firefox 가능
<source lang='html'>
<syntaxhighlight lang='html' run>
<input id="myInput" value="Hello, World!">
<input id="myInput" value="Hello, World!">
<button onclick="copy_to_clipboard()">클립보드로 복사</button>
<button onclick="copy_to_clipboard()">클립보드로 복사</button>
37번째 줄: 35번째 줄:
   copyText.select();
   copyText.select();
   document.execCommand("Copy");
   document.execCommand("Copy");
  console.log('Copied!');
}
}
</script>
</script>
</source>
</syntaxhighlight>
<jsfiddle>anz43t97</jsfiddle>


==예시 3: 구식 IE==
==예시 3: 구식 IE==
* 인터넷 익스플로러만 가능
* 인터넷 익스플로러만 가능
<source lang='html'>
<syntaxhighlight lang='html' run>
<script type="text/javaScript">
<script type="text/javaScript">
function copy_to_clipboard(str) {
function copy_to_clipboard(str) {
53번째 줄: 51번째 줄:


<button onclick="copy_to_clipboard('Hello Jmnote');">복사</button>
<button onclick="copy_to_clipboard('Hello Jmnote');">복사</button>
</source>
</syntaxhighlight>
 
==예시 4: 구식 자동·수동 혼합==
*익스플로러가 아닐 경우 [[prompt]]를 띄워 수동으로 복사(Ctrl+C)하도록 함
<source lang='html5'>
<script type="text/javaScript">
function is_ie() {
  if(navigator.userAgent.toLowerCase().indexOf("chrome") != -1) return false;
  if(navigator.userAgent.toLowerCase().indexOf("msie") != -1) return true;
  if(navigator.userAgent.toLowerCase().indexOf("windows nt") != -1) return true;
  return false;
}
function copy_to_clipboard(str) {
  if( is_ie() ) {
    window.clipboardData.setData("Text", str);
    alert("복사되었습니다.");
    return;
  }
  prompt("Ctrl+C를 눌러 복사하세요.", str);
}
</script>
<button onclick="copy_to_clipboard('Hello Jmnote2');">복사</button>
</source>


==같이 보기==
==같이 보기==

2021년 4월 22일 (목) 00:39 기준 최신판

1 개요

JavaScript 클립보드로 복사하기
자바스크립트 클립보드 복사

2 예시 1: textarea 임시 생성하여 복사 ★

<button onclick="copy()">COPY</button>

<script>
function copyToClipboard(val) {
  const t = document.createElement("textarea");
  document.body.appendChild(t);
  t.value = val;
  t.select();
  document.execCommand('copy');
  document.body.removeChild(t);
}
function copy() {
  copyToClipboard('Hello World');
  console.log('Copied!');
}
</script>

3 예시 2: input에서 복사

  • IE 10+, Chrome 43+, Opera 29+, Firefox 가능
<input id="myInput" value="Hello, World!">
<button onclick="copy_to_clipboard()">클립보드로 복사</button>

<script>
function copy_to_clipboard() {
  var copyText = document.getElementById("myInput");
  copyText.select();
  document.execCommand("Copy");
  console.log('Copied!');
}
</script>

4 예시 3: 구식 IE

  • 인터넷 익스플로러만 가능
<script type="text/javaScript">
function copy_to_clipboard(str) {
  window.clipboardData.setData("Text", str);
  alert("복사되었습니다.");
}
</script>

<button onclick="copy_to_clipboard('Hello Jmnote');">복사</button>

5 같이 보기

6 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}