Javascript

[javascript] 버튼으로 현재 URL 복사하기

유호야 2021. 6. 14. 00:56
반응형

 

 

 

Copy current URL to clipboard

Not sure why this has been so difficult for me today, but for some reason I cannot seem to get it to copy the current URL to the clipboard. Overall, I'm looking for a way to do it without needing to

stackoverflow.com

 

  function CopyUrlToClipboard(){
        var dummy   = document.createElement("input");
        var text    = location.href;
        
        document.body.appendChild(dummy);
        dummy.value = text;
        dummy.select();
        document.execCommand("copy");
        document.body.removeChild(dummy);
    }

일시적으로 input element를 생성해서 값을 클립보드에 복사하고 바로 삭제한다.

 

 

출처 : https://emessell.tistory.com/164

 

반응형