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

20번째 줄: 20번째 줄:
<jsfiddle>anz43t97</jsfiddle>
<jsfiddle>anz43t97</jsfiddle>


==예시 2==
==예시 2: 구식 IE==
* 인터넷 익스플로러만 가능
* 인터넷 익스플로러만 가능
<source lang='html5'>
<source lang='html5'>

2017년 12월 13일 (수) 17:51 판

1 개요

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

2 예시 1 ★

  • 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");
}
</script>

3 예시 2: 구식 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>

4 예시 3: 자동 수동 혼합

<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>

5 같이 보기

6 참고

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