I know this kind of question has been asked here for many times, including: How do I copy to the clipboard in JavaScript? or How to copy text to the client's clipboard using jQuery?, I'm narrowing the scope:
Condition:
- works fine in Google Chrome (would be nice if cross-browser, but not necessary)
- with no flash
Is there such a solution or workaround?
Variable string can copy to clipboard using below js code.
Then call the function with the text and that should work.
Actually for those figuring this out i got it to work in chrome based on @JulianGregoire s answer.
I rewrote the code to make it a bit better in my opinion:
Beware: I tried the exact same script of Julien Grégoire, however it wasn't triggering the oncopy event listener. The reason, I had user-select CSS for the body-tag.
So make sure you remove it, or set it to
initial
on the element the event listener is attached to.Example: https://jsfiddle.net/faimmedia/jsLfnnvy/80/
If you're using knockout, like me (for some reason I still am), you'll want to take a look at this question/answer:
How to make KnokoutJS and ClipboardJS work together?
You can use either
document.execCommand('copy')
oraddEventListener('copy')
, or a combination of both.1. copy selection on custom event
If you want to trigger a
copy
on some other event thanctrl-c
or right click copy, you usedocument.execCommand('copy')
. It'll copy what's currently selected. Like this, on mouseup for example:EDIT:
document.execCommand('copy'
) is supported only byChrome 42
,IE9
andOpera 29
, but will be supported by firefox 41 (scheduled for september 2015). Note that IE will normally asks for permission to access the clipboard.https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
2. copy custom content on copy triggered by user
Or, you can use
addEventListener('copy')
, this will interfere with copy event and you can put the content you want there. This suppose user triggers copy.EDIT:
On Chrome, Firefox and Safari the event has the
clipboardData
object withsetData
method. OnIE
, theclipboardData
object is a window property. So this can work on all major browsers provided you validate where isclipboardData
.https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboardData https://msdn.microsoft.com/en-us/library/ms535220(v=vs.85).aspx
3. a bit of both
Using a combination, you can copy custom content on wanted events. So the first event triggers
execCommand
, then thelistener
interferes. For example, put custom content on click on a div.Using this last one supposes that both approach are supported, as of July 2015, it works only on
Chrome 43
(maybe 42 I couldn't test) andIE
at least 9 and 10. WithFirefox 41
supportingexeccommand('copy')
, it should work as well.Note that for most of these methods and properties are declared as either experimental (or even deprecated for IE), so it's to be used carefully, but it looks like it's more and more supported.
Fiddle with all examples: https://jsfiddle.net/jsLfnnvy/12/