JS for browser-compatible copy function

Source: Internet
Author: User

You often see the effect of clicking a button to copy the contents of an area to the Clipboard. In fact, this function is not difficult to achieve, the core is to use the window Sub-object clipboarddata a method:setData ()
Grammar:
Clipboarddata.setdata (Sdataformat, SData)
Parameters: Sdataformat: The format of the content to be copied; SData: what to copy.
Return value: Copy succeeded return true; Failed returns false.

<script language= "JavaScript" >
function Jianqie (ID)
{var id;
var text=document.all (id). innerText
if (Clipboarddata.setdata ("text", text))
{alert ("Copy succeeded!")}
Else
{alert ("Copy failed!")}
}
</script>

IE JS Code:

Copy CodeThe code is as follows:
<script type= "Text/javascript" >
function CopyUrl2 ()
{
var Url2=document.getelementbyid ("Biao1");
Url2.select (); Select Object
Document.execcommand ("Copy"); Perform a browser copy command
Alert ("Copied well, can be glued. ");
}
</script>
<textarea cols= "rows=" id= "Biao1" > user-defined code area </textarea>
<input type= "button" onclick= "CopyUrl2 ()" value= "click Copy Code"/>


Chrome JS Code:

Copy CodeThe code is as follows:
<script src= "Http://www.weicaiyun.com/assets/js/copy/ZeroClipboard.js" ></script>

<script type= "Text/javascript" >

var clip = new Zeroclipboard.client ();
Clip.sethandcursor (TRUE);
Clip.settext (' copy content ');
Clip.glue (' button ');

</script>

<input type= "button" id= "button" value= "click Copy Code"/>

http://zeroclipboard.org/#demo//zeroclipboard

http://blog.csdn.net/skyboxgogo/article/details/8768683 Detailed-----

JS for browser-compatible copy function

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.