0

Something as simple as this?

let myString = "foo";

copyToClipboard(myString);

Chris
  • 679
  • 1
  • 11
  • 26
  • 1
    the OP said he didn't want to use execCommand (wondering why....) – Pierre Feb 06 '20 at 16:46
  • 2
    https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write – Teemu Feb 06 '20 at 16:47
  • in safari, edge and internet explorer? – Estradiaz Feb 06 '20 at 16:49
  • I was thinking execCommand('copy') required coping a manipulated selection of text, which seemed error prone compared to just giving it the text I already know. But if that's the best way it's ok. I'd like it to work pretty universally, yeah. – Chris Feb 06 '20 at 18:05
  • I found this post which worked for me. https://stackoverflow.com/a/49121680/7636192 – Chris Feb 06 '20 at 18:14

1 Answers1

0

There are two ways to interact with the clipboard:

  • document.execCommand("copy") < Which you said you don't want to use.
  • navigator.clipboard.write

The support for the more modern second option is lacking though: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write

There is currently no other supported way :)

basarat
  • 261,912
  • 58
  • 460
  • 511