JS content Copy to Clipboard method summary

Source: Internet
Author: User

In JS there is a clipboarddata.setdata (Sdataformat, sData) function that can be implemented

The code is as follows Copy Code

Clipboarddata.setdata ("text", www.111cn.net)

<a href= "#" Onclick=javascript:clipboarddata.setdata ("text", "Www.111cn.net") >
Clicking on the current link text indicates that the content has been copied to the local clipboard, and the definition can be displayed simply by "Ctrl" + "V"
</a>

How to get "Copy selection", which requires the use of the Document.selection.createRange () method

The code is as follows Copy Code
<script type= "Text/javascript" >
Document.body.oncopy=function () {
Event.returnvalue=false;
var t=document.selection.createrange (). text;
var s= "About clicking the button to copy content to the Clipboard's original content address:" +LOCATION.HREF;
Clipboarddata.setdata (' Text ', t+ ' rnrn ' +s+ ' rn ');
}
</script>

The above method can be used in ie,ff but sometimes in some browsers will not be compatible oh, let me introduce a better solution

Zero Clipboard, using this JS library can support the use of Flash 10 implementation to copy to the Clipboard. The idea is to overwrite a DOM element such as a button or div on a transparent flash (invisible to the user), and when clicked on the DOM, it actually clicks on Flash, which accesses the Flash clipboard.

The code is as follows Copy Code
<title>zero Clipboard test</title>
<script type= "Text/javascript" src= "Zeroclipboard.js" ></script>
<script language= "JavaScript" >
var clip = null;
function $ (ID) {return document.getElementById (ID);}
function init () {
Clip = new Zeroclipboard.client ();
Clip.sethandcursor (TRUE);
Clip.addeventlistener (' MouseOver ', function (client) {
Update the text on mouse over
Clip.settext ($ (' Fe_text '). Value);
});

Clip.addeventlistener (' Complete ', function (client, text) {
Debugstr ("Copied Text to Clipboard:" + text);
Alert ("The address has been copied and you can paste it using CTRL + V.") ");
});
Clip.glue (' Clip_button ', ' Clip_container ');
}
</script>
<body onload= "init ()" >
<input id= "Fe_text" cols=50 rows=5 value= Copy content Text 1 >
<span id= "Clip_container" ><span id= "Clip_button" ><b> replication </b></span></span>
</body>

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.