LoginSignup
13
15

More than 5 years have passed since last update.

JavaScript でクリップボードを操作する方法

Posted at
  • document.execCommand('copy'); で選択中の文字列をコピー出来ます
  • document.execCommand('cut'); で選択中の文字列をカット出来ます
  • document.execCommand('paste'); でペースト出来ます

従来の問題

従来は JavaScript だけでクリップボードを操作することは難しくて、

  • Flash を使う
  • window.clipboardData という IE 独自のオブジェクトを使う

といった方法が一般的に取られていました。

document.execCommand について

  • 他にもテキストの書体(bold 等)を変更できたり
  • document.execCommand('insertHTML', false, foo); で HTML 文字列を挿入できたり

と色々出来ます。

詳細は MDN のページにあります。

ブラウザの対応状況については MDN のページの「ブラウザの互換性」をご覧ください。
最近のブラウザなら使えるようです。

13
15
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
13
15