Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy to the clipboard in JavaScript?

What is the best way to copy text to the clipboard (multi-browser)?

I have tried:

function copyToClipboard(text) {     if (window.clipboardData) { // Internet Explorer         window.clipboardData.setData("Text", text);     } else {         unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");         const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);         clipboardHelper.copyString(text);     } } 

But in Internet Explorer it gives a syntax error. In Firefox, it says unsafeWindow is not defined.

A nice trick without using Flash: How does Trello access the user's clipboard?

like image 575
Santiago Corredoira Avatar asked Dec 30 '08 13:12

Santiago Corredoira


People also ask

What is clipboard in JavaScript?

Clipboard is based on the EventTarget interface, and includes its methods. read() Requests arbitrary data (such as images) from the clipboard, returning a Promise that resolves with an array of ClipboardItem objects containing the clipboard's contents. readText()

How do I copy text to clipboard?

To quickly copy selected text or images to the clipboard, use hotkeys Ctrl+C or Ctrl+Insert. These hotkeys work in all Windows programs. Alternatively, you can invoke a pop-up menu by right-clicking on the selected text, and then click Copy.

How do you add to your clipboard?

Highlight the text, long-press the selected text, then choose Copy. Long-press an empty field and select Paste to insert the copied text. Alternative method: Use the Gboard keyboard to manage the clipboard.


1 Answers

Overview

There are three primary browser APIs for copying to the clipboard:

  1. Async Clipboard API [navigator.clipboard.writeText]

    • Text-focused portion available in Chrome 66 (March 2018)
    • Access is asynchronous and uses JavaScript Promises, can be written so security user prompts (if displayed) don't interrupt the JavaScript in the page.
    • Text can be copied to the clipboard directly from a variable.
    • Only supported on pages served over HTTPS.
    • In Chrome 66 pages inactive tabs can write to the clipboard without a permissions prompt.
  2. document.execCommand('copy') (deprecated) 👎

    • Most browsers support this as of ~April 2015 (see Browser Support below).
    • Access is synchronous, i.e. stops JavaScript in the page until complete including displaying and user interacting with any security prompts.
    • Text is read from the DOM and placed on the clipboard.
    • During testing ~April 2015 only Internet Explorer was noted as displaying permissions prompts whilst writing to the clipboard.
  3. Overriding the copy event

    • See Clipboard API documentation on Overriding the copy event.
    • Allows you to modify what appears on the clipboard from any copy event, can include other formats of data other than plain text.
    • Not covered here as it doesn't directly answer the question.

General development notes

Don't expect clipboard related commands to work whilst you are testing code in the console. Generally, the page is required to be active (Async Clipboard API) or requires user interaction (e.g. a user click) to allow (document.execCommand('copy')) to access the clipboard see below for more detail.

IMPORTANT (noted here 2020/02/20)

Note that since this post was originally written deprecation of permissions in cross-origin IFRAMEs and other IFRAME "sandboxing" prevents the embedded demos "Run code snippet" buttons and "codepen.io example" from working in some browsers (including Chrome and Microsoft Edge).

To develop create your own web page, serve that page over an HTTPS connection to test and develop against.

Here is a test/demo page which demonstrates the code working: https://deanmarktaylor.github.io/clipboard-test/

Async + Fallback

Due to the level of browser support for the new Async Clipboard API, you will likely want to fall back to the document.execCommand('copy') method to get good browser coverage.

Here is a simple example (may not work embedded in this site, read "important" note above):

function fallbackCopyTextToClipboard(text) {   var textArea = document.createElement("textarea");   textArea.value = text;      // Avoid scrolling to bottom   textArea.style.top = "0";   textArea.style.left = "0";   textArea.style.position = "fixed";    document.body.appendChild(textArea);   textArea.focus();   textArea.select();    try {     var successful = document.execCommand('copy');     var msg = successful ? 'successful' : 'unsuccessful';     console.log('Fallback: Copying text command was ' + msg);   } catch (err) {     console.error('Fallback: Oops, unable to copy', err);   }    document.body.removeChild(textArea); } function copyTextToClipboard(text) {   if (!navigator.clipboard) {     fallbackCopyTextToClipboard(text);     return;   }   navigator.clipboard.writeText(text).then(function() {     console.log('Async: Copying to clipboard was successful!');   }, function(err) {     console.error('Async: Could not copy text: ', err);   }); }  var copyBobBtn = document.querySelector('.js-copy-bob-btn'),   copyJaneBtn = document.querySelector('.js-copy-jane-btn');  copyBobBtn.addEventListener('click', function(event) {   copyTextToClipboard('Bob'); });   copyJaneBtn.addEventListener('click', function(event) {   copyTextToClipboard('Jane'); });
<div style="display:inline-block; vertical-align:top;">   <button class="js-copy-bob-btn">Set clipboard to BOB</button><br /><br />   <button class="js-copy-jane-btn">Set clipboard to JANE</button> </div> <div style="display:inline-block;">   <textarea class="js-test-textarea" cols="35" rows="4">Try pasting into here to see what you have on your clipboard:    </textarea> </div>

(codepen.io example may not work, read "important" note above) Note that this snippet is not working well in Stack Overflow's embedded preview you can try it here: https://codepen.io/DeanMarkTaylor/pen/RMRaJX?editors=1011

Async Clipboard API

  • MDN Reference
  • Chrome 66 announcement post (March 2018)
  • Reference Async Clipboard API draft documentation

Note that there is an ability to "request permission" and test for access to the clipboard via the permissions API in Chrome 66.

var text = "Example text to appear on clipboard"; navigator.clipboard.writeText(text).then(function() {   console.log('Async: Copying to clipboard was successful!'); }, function(err) {   console.error('Async: Could not copy text: ', err); }); 

document.execCommand('copy')

The rest of this post goes into the nuances and detail of the document.execCommand('copy') API.

Browser Support

The JavaScript document.execCommand('copy') support has grown, see the links below for browser updates: (deprecated) 👎

  • Internet Explorer 10+ (although this document indicates some support was there from Internet Explorer 5.5+).
  • Google Chrome 43+ (~April 2015)
  • Mozilla Firefox 41+ (shipping ~September 2015)
  • Opera 29+ (based on Chromium 42, ~April 2015)

Simple Example

(may not work embedded in this site, read "important" note above)

var copyTextareaBtn = document.querySelector('.js-textareacopybtn');  copyTextareaBtn.addEventListener('click', function(event) {   var copyTextarea = document.querySelector('.js-copytextarea');   copyTextarea.focus();   copyTextarea.select();    try {     var successful = document.execCommand('copy');     var msg = successful ? 'successful' : 'unsuccessful';     console.log('Copying text command was ' + msg);   } catch (err) {     console.log('Oops, unable to copy');   } });
<p>   <button class="js-textareacopybtn" style="vertical-align:top;">Copy Textarea</button>   <textarea class="js-copytextarea">Hello I'm some text</textarea> </p>

Complex Example: Copy to clipboard without displaying input

The above simple example works great if there is a textarea or input element visible on the screen.

In some cases, you might wish to copy text to the clipboard without displaying an input / textarea element. This is one example of a way to work around this (basically insert an element, copy to clipboard, remove element):

Tested with Google Chrome 44, Firefox 42.0a1, and Internet Explorer 11.0.8600.17814.

(may not work embedded in this site, read "important" note above)

function copyTextToClipboard(text) {   var textArea = document.createElement("textarea");    //   // *** This styling is an extra step which is likely not required. ***   //   // Why is it here? To ensure:   // 1. the element is able to have focus and selection.   // 2. if the element was to flash render it has minimal visual impact.   // 3. less flakyness with selection and copying which **might** occur if   //    the textarea element is not visible.   //   // The likelihood is the element won't even render, not even a   // flash, so some of these are just precautions. However in   // Internet Explorer the element is visible whilst the popup   // box asking the user for permission for the web page to   // copy to the clipboard.   //    // Place in the top-left corner of screen regardless of scroll position.   textArea.style.position = 'fixed';   textArea.style.top = 0;   textArea.style.left = 0;    // Ensure it has a small width and height. Setting to 1px / 1em   // doesn't work as this gives a negative w/h on some browsers.   textArea.style.width = '2em';   textArea.style.height = '2em';    // We don't need padding, reducing the size if it does flash render.   textArea.style.padding = 0;    // Clean up any borders.   textArea.style.border = 'none';   textArea.style.outline = 'none';   textArea.style.boxShadow = 'none';    // Avoid flash of the white box if rendered for any reason.   textArea.style.background = 'transparent';     textArea.value = text;    document.body.appendChild(textArea);   textArea.focus();   textArea.select();    try {     var successful = document.execCommand('copy');     var msg = successful ? 'successful' : 'unsuccessful';     console.log('Copying text command was ' + msg);   } catch (err) {     console.log('Oops, unable to copy');   }    document.body.removeChild(textArea); }   var copyBobBtn = document.querySelector('.js-copy-bob-btn'),   copyJaneBtn = document.querySelector('.js-copy-jane-btn');  copyBobBtn.addEventListener('click', function(event) {   copyTextToClipboard('Bob'); });   copyJaneBtn.addEventListener('click', function(event) {   copyTextToClipboard('Jane'); });
<div style="display:inline-block; vertical-align:top;">   <button class="js-copy-bob-btn">Set clipboard to BOB</button><br /><br />   <button class="js-copy-jane-btn">Set clipboard to JANE</button> </div> <div style="display:inline-block;">   <textarea class="js-test-textarea" cols="35" rows="4">Try pasting into here to see what you have on your clipboard:    </textarea> </div>

Additional notes

Only works if the user takes an action

All document.execCommand('copy') calls must take place as a direct result of a user action, e.g. click event handler. This is a measure to prevent messing with the user's clipboard when they don't expect it.

See the Google Developers post here for more info.

Clipboard API

Note the full Clipboard API draft specification can be found here: https://w3c.github.io/clipboard-apis/

Is it supported?

  • document.queryCommandSupported('copy') should return true if the command "is supported by the browser".
  • and document.queryCommandEnabled('copy') return true if the document.execCommand('copy') will succeed if called now. Checking to ensure the command was called from a user-initiated thread and other requirements are met.

However, as an example of browser compatibility issues, Google Chrome from ~April to ~October 2015 only returned true from document.queryCommandSupported('copy') if the command was called from a user-initiated thread.

Note compatibility detail below.

Browser Compatibility Detail

Whilst a simple call to document.execCommand('copy') wrapped in a try/catch block called as a result of a user click will get you the most compatibility use the following has some provisos:

Any call to document.execCommand, document.queryCommandSupported or document.queryCommandEnabled should be wrapped in a try/catch block.

Different browser implementations and browser versions throw differing types of exceptions when called instead of returning false.

Different browser implementations are still in flux and the Clipboard API is still in draft, so remember to do your testing.

like image 157
Dean Taylor Avatar answered Sep 23 '22 06:09

Dean Taylor