Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy text to the clipboard in Javascript?

I'm wondering if there's any way to copy text to the clipboard. I'm well aware of this answer, but it's over three years old now. Has anything changed since then?

like image 286
Elliot Bonneville Avatar asked Mar 20 '12 19:03

Elliot Bonneville


People also ask

How do I copy to the clipboard in JavaScript?

To copy text with the new Clipboard API, you will use the asynchronous writeText() method. This method accepts only one parameter - the text to copy to your clipboard. This can be a string, a template literal holding variables and other strings, or a variable used to hold a string.

How do I copy text to clipboard?

Copying Text to the ClipboardCTRL+C to copy. CTRL+X to cut. CTRL+V to paste.

How do you copy and paste text in JavaScript?

JavaScript offers some built-in clipboard properties that allow us to copy/paste the text into the clipboard using a single button click. In JavaScript, the navigator. clipboard is used to get access to the clipboard while writeText() property is used to copy the text into the clipboard.

Can JavaScript access the clipboard?

Accessing the OS clipboard using browser JavaScript has been possible for several years using document. execCommand() . Unfortunately, there are some problems: clipboard access is synchronous, which has performance and security implications.


1 Answers

The easiest thing to do at this point is to go with a Flash based solution. zeroclipboard is a common one (a nice walkthrough is available here).

Browser vendors have over the past few years removed programatic access to the clipboard. Safari / Chrome lost the ability after a change in WebKit, and FireFox for a long time has blocked it. Only IE remains as one that does allow it, however it displays an alert on each page initially.

like image 140
vcsjones Avatar answered Sep 28 '22 12:09

vcsjones