웹프로그래밍/자바스크립트
버튼 클릭으로 소스 복사하기
노을빛호랑이
2007. 12. 19. 18:23
반응형
웹페이지에서 특정한 textarea 에 기록되어 있는 내용을 버튼을 클릭하여 방문자의 클립보드에 복사해주는 기능이다.
복사를 할 textarea의 id를 pagelink 또는 원하는 아이디로 맞춰준다.
그리고 버튼에 onclick 함수를 사용하여 위의 text_copy()를 호출한다.
위처럼 함수(text_copy)를 선언해 준뒤,<script type="text/JavaScript">
function text_copy(){var doc = document.body.createTextRange();
doc.moveToElementText(document.all("pagelink"));
doc.select();doc.execCommand('copy');
// js코드가 업데이트 되었습니다.
var doc = document.createElement('textarea');
doc.textContent = document.getElementById("#pagelink").textContent;
document.body.append(doc);
doc.select();
document.execCommand('copy');
doc.remove();
alert('소스가 저장되었습니다. 붙여넣기 하시면 됩니다.');
}
</script>
복사를 할 textarea의 id를 pagelink 또는 원하는 아이디로 맞춰준다.
그리고 버튼에 onclick 함수를 사용하여 위의 text_copy()를 호출한다.
예)
<textarea name="pagelink" cols="70" rows="1" id="pagelink">이글이 복사됩니다</textarea>
<input name="button" type="button" onclick="text_copy();" value="복사하기">
반응형