본문 바로가기
웹 프로그래밍

[JS] URL COPY TO CLIPBOARD, URL 주소 클립보드에 복사하기

by Minius 2020. 6. 3.
반응형

https://stackoverflow.com/questions/49618618/copy-current-url-to-clipboard

 

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를 생성해서 값을 클립보드에 복사하고 바로 삭제한다.

댓글