if
statement to ensure Navigator
, Navigator.clipboard
and Navigator.clipboard.writeText
are truthy.Clipboard.writeText()
to write the given value, str
, to the clipboard.Clipboard.writeText()
, which is a promise that resolves when the clipboard's contents have been updated.Promise.reject()
to reject with an appropriate message.Document.execCommand('copy')
instead. You can find out more about it in the copyToClipboard snippet.const copyToClipboardAsync = str => { if (navigator && navigator.clipboard && navigator.clipboard.writeText) return navigator.clipboard.writeText(str); return Promise.reject('The Clipboard API is not available.'); };
copyToClipboardAsync('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️