javascript - Copy to clipboard with jQuery/js in Chrome -
I know that these questions have been asked here many times, including: or, I reduce the scope
Condition:
- Google Chrome works fine (if cross-browser, but not necessary if necessary)
- No flash
Is there such a solution or solution?
You can either
Document.execCommand ('copy') or
AddEventListener ('copy') , or combination of both. Copy selection to
1 custom event
If you compare
ctrl-c to another event
document.execCommand ('copy') . This will copy what is currently selected, for example on mouseup:
elem.onmouseup = function () {document.execCommand ('copy'); }
Edit:
document.execCommand ('copy' ) is only supported by
Chrome 42 ,
IE9 and
Opera 29 , but will be supported by Firefox 41 (scheduled for September 2015). Note that IE generally requires permission to use the clipboard.
2
Copy the custom content on the user triggered trigger or you
addEventListener ('copy ') , this copy will interfere with the event and you assume the content you want there is that the user makes a copy.
Edit:
The
clipboard data object
setData method on Chrome, Firefox and Safari. On
IE , the
clipboard data object is a window property so it can work on all major browsers, where you can verify that the
clipboard data < / Code> Where is it.
elem2.addEventListener ('copy', function (e) {e.preventDefault (); if (e.clipboard data) {e.clipboardData.setData ('text / plain', 'custom Content ');} and if (window.clipboardData) {window.clipboardData.setData (' text ',' custom content ');}}); Using a combination of
3 a little bit of both
, custom content on the event you want If the copy can be copied then the first event trigger
execCommand , then
the listener interferes. For example, if you click on a single device, add custom content.
elem3.onclick = function () {document.execCommand ('copy'); } Elem3.addEventListener ('copy', function (e) {e.preventDefault (); if (e.clipboardData) {e.clipboardData.setData ('text / plain', 'click from custom content');} else if (window.clipboardData) {window.clipboardData.setData ('text', 'click to custom content');}});
This end use estimates that both approaches are supported on July 2015, it only works on
Chrome 43 (Maybe 42 I could not test ) And with
IE
firefox 41 assistant
execcommand ('copy') with at least 9 and 10, it should also work.
Note that for most of these methods and properties is declared as experimental (or also deprecated for IE), so it has to be used with caution, but it seems that this More than supported.
All examples:
Comments
Post a Comment