Javascript-how to copy text in javascript

Source: Internet
Author: User
Simply click Copy, as if many websites have the copy code button. However, it seems that many Browsers Do not support flash. Now, how to solve this problem is to simply click Copy, as if many websites have this button. But it seems that many Browsers Do not support flash. How can this problem be solved now?

Reply content:

Just click Copy. It seems that many websites have this button. But it seems that many Browsers Do not support flash. How can this problem be solved now?

For long ago, flash needs to be introduced to access the clipboard by calling the flash api, Which is disgusting. Now, abyun does not recommend flash itself, so there is a natural way.

Https://zenorocha.github.io/clipboard.js/
Depende onSelectionAnddocument.execCommandAPI

SCRIPT function CopyAll (T) {T. focus () T. select () if (document.all?therange=t.createtextrange(thertherange.exe cCommand ("Copy") // Copy} SCRIPT(Content test)(You can paste the content here)

He JavaScript document.exe cCommand ('copy') support has grown, see the links below for browser updates:

IE10 + (although this document indicates some support was there from IE5.5 + ).
Google Chrome 43 + (~ April 2015)
Mozilla Firefox 41 + (shipping ~ September 2015)
Opera 29 + (based on Chromium 42 ,~ April 2015)
Simple Example
Var copyTextareaBtn = document. querySelector ('. js-textareacopybtn ');

CopyTextareaBtn. addEventListener ('click', function (event ){
Var copyTextarea = document. querySelector ('. js-copytextarea ');
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');

}
});


Hello I'm some text


Copy Textarea

See http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.