1) navigator.clipboard.writeText()
navigator.clipboard.writeText라는 method를 사용할 것이다.
사용 방법은 다음과 같다.
var text = "저장하고 싶은 문자열";
navigator.clipboard.writeText(text)
.then(() => console.log("클립보드에 성공적으로 복사되었습니다."))
.catch(error => console.error("클립보드에 복사하는 데 실패했습니다: ", error));
이 기능은 HTTPS나 Localhost에서만 사용이 가능하다고 하며
자세한 정보는 공식 문서를 확인하면 좋다.
링크 : https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText
Clipboard: writeText() method - Web APIs | MDN
The writeText() method of the Clipboard interface writes the specified text to the system clipboard, returning a Promise that is resolved once the system clipboard has been updated.
developer.mozilla.org
2) document.execCommand()
document.execCommand()라는 JavaScript API도 있다.
이는 권장되지 않는 방법이지만 알아만 두자.
// 기본적인 사용법
document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)
aCommandName: 실행하려는 명령 이름(copy, bold, cut, delete 등)
aShowDefaultUI: 그냥 false라고 생각하면 편하다.
aValueArgument: 명령에 따라 필요한 경우가 있다.(명령 이름이 createLink인 경우 URL을 넣어야 함)
링크 : https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
Document: execCommand() method - Web APIs | MDN
The execCommand method implements multiple different commands. Some of them provide access to the clipboard, while others are for editing form inputs, contenteditable elements or entire documents (when switched to design mode).
developer.mozilla.org
'💻 소프트웨어(SW) > HTML | JS | CSS' 카테고리의 다른 글
[JS] [object Object] 형태 출력하기 (0) | 2023.09.05 |
---|---|
[JS] input이나 textarea에서 커서 위치 확인하기 (0) | 2023.09.04 |
[CSS] 가상 선택자(Hover, Active, Focus, Visited 등) (0) | 2023.08.13 |
[HTML] a태그 새창으로 여는 방법 (0) | 2023.08.12 |
[CSS] text gradient 텍스트 그라데이션 효과 넣기 (0) | 2022.08.11 |