Copy selected text to the clipboard WITHOUT using flash - must be cross-browser
I want to have a button which selects the text in a textarea
and copies it to the clipboard. I can't seem to find any solutions which work in all browsers and don't use flash.
Surely this is doable? I've seen it all over the place but I guess they use flash, which I really want to stay away from if possible as some people don't have it.
This is what I have so far - it just selects the text:
function copyCode() {
$("#output-code").focus();
$("#output-code").select();
}
(The focus is not strictly necessary)
Solution 1:
execCommand('copy')
There is a very new option. It is cross-browser but it will take time until everyone has updated their browser.
It works by using the document.execCommand('copy');
function.
With this function you'll copy the select text. This will not only work with textarea
s but with every selected text on the webpage (like in span
, p
, div
, etc.).
Available in Internet Explorer 10+, Chrome 43+, Opera 29+ and Firefox 41+ (see execCommand
compatibility here).
Example
// Setup the variables
var textarea = document.getElementById("textarea");
var answer = document.getElementById("copyAnswer");
var copy = document.getElementById("copyBlock");
copy.addEventListener('click', function(e) {
// Select some text (you could also create a range)
textarea.select();
// Use try & catch for unsupported browser
try {
// The important part (copy selected text)
var ok = document.execCommand('copy');
if (ok) answer.innerHTML = 'Copied!';
else answer.innerHTML = 'Unable to copy!';
} catch (err) {
answer.innerHTML = 'Unsupported Browser!';
}
});
<textarea id="textarea" rows="6" cols="40">
Lorem ipsum dolor sit amet, eamsemper maiestatis no.
</textarea><br/>
<button id="copyBlock">Click to copy</button> <span id="copyAnswer"></span>
Solution 2:
This answer, while accurate in 2011, is now considerably out of date. See arc's answer, or https://stackoverflow.com/a/30810322/489560
You must use the Flash add-in you do not want to use to automatically copy text to the client's clipboard. Browsers are designed like this because a website automatically modifying the client's clipboard without the aid of active-x components is a security concern. Note that active-x components are programs that run on the user's machine and, technically, require the user's consent to be installed. Considering that the Clipboard is an operating system component, be happy that web browsers don't allow websites to highjack it by default.
If the user does not have Flash, has Flash disabled, or has active-x disabled, then he or she probably is paranoid about security and doesn't want you messing with his or her keyboard anyway. At that point, the user would be used to not having much automatic or script-based functionality in websites. It's best to not try to openly defy the wishes of the end user.
Please refer to the following Stack Overflow links:
- How do I copy to the clipboard in JavaScript?
- Cross Browser Flash Detection in Javascript
The ultimate answer there is to use Zero Clipboard, which is a library that uses a small, invisible Flash movie and JavaScript to use clipboard functionality like you are wanting. The library is available here: https://github.com/zeroclipboard/zeroclipboard The second link shows how to detect if Flash is disabled or not installed, which allows you to display a warning message like you would for JavaScript.
Solution 3:
Now we have Clipboard.js by @zenorocha
To use it, download and call the script on your page.html (or install with bower or npm)
<script src="path_to_script/clipboard.min.js"></script>
Instantiate a new trigger on your script.js
new Clipboard('.trigger');
And go there to see some examples of usage: http://zenorocha.github.io/clipboard.js/#usage