38
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScript: document.execCommand("copy") の代替

Last updated at Posted at 2021-06-07

前提

document.execCommand("copy")

が既に非推奨になっている。代替手段として以下。

  • HTML5のClipboard API
  • IE対応ではwindow.clipboardData

Clipboard API(IE以外)

navigator.clipboard.writeText()

var clipboardText = "clipboard";
navigator.clipboard.writeText(clipboardText);

IE以外は対応している。
*Compatibility

補足

window.clipboardData(IE)

window.clipboardData.setData

var clipboardText = "clipboard";
window.clipboardData.setData('Text', clipboardText ); 

両方対応する場合

var clipboardText = "clipboard";
if(navigator.clipboard == undefined) {
    window.clipboardData.setData('Text', clipboardText);
} else {
    navigator.clipboard.writeText(clipboardText);
}

参考

こんなのもある。
以上メモのみ。

38
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?