자바스크립트(javascript) 클립보드에 복사.
클립보드 복사가 정상적으로 수행이 되지 않을시 수동으로 프롬포트 띄워 복사를 가능하게 끔 한다.
function copyLink() {
var result = true;
var targetText = arguments[0]; // 복사할 text
if(window.clipboardData){
window.clipboardData.setData('text', targetText); // for IE
}else{
var docArea = document.getElementById("clipboardArea");
docArea.parentNode.style.display = "block";
docArea.value = targetText;
var rangeToSelect = document.createRange();
rangeToSelect.selectNodeContents( docArea );
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange( rangeToSelect );
try {
if ( window.netscape && (netscape.security && netscape.security.PrivilegeManager) ){
netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" );
}
docArea.select();
result = document.execCommand( "copy", false, null );
}
catch ( error ){
result = false;
console.log( error );
}
docArea.parentNode.style.display = "none";
docArea.value = "";
}
if(result) alert("클립보드에 복사되었습니다. \n 'Ctrl+v'를 사용하여 원하는 곳에 붙여넣기 하세요. ")
else window.prompt ("클립보드로 복사: Ctrl+C, Enter", targetText);
}
참조.
http://tonks.tistory.com/143 : 웹마스터를 향하여