Copy to Clipboard JS code compatible with IE and Firefox

Source: Internet
Author: User

The following code is my own most common

The code is as follows Copy Code

<title>js Copy to Clipboard </title>
<script>
function Showdivlinkcopy () {
var Msg=document.getelementbyid ("text"). Value;
Window.clipboardData.setData ("Text", msg);
}
</script>
<body>
<input name= "" type= "text" id= "text" value= "click on the right copy" style= "border:1px solid #ccc; padding:4px; font-size:14px; Color: #444; width:200px; line-height:20px; "/>
<a href= "javascript:void (0);" onclick= "Showdivlinkcopy ()" style= "padding-left:15px; line-height:30px; font-size:16px; " > Replication </a>
</body>

But use up in IE without any problems, if the FF browser does not respond, and then Baidu a better compatibility copy to the Clipboard
JS Code

This script is passed a string to CopyToClipboard and it will be copied text that is placed on the Clipboard and you can paste it into another program.

The code is as follows Copy Code

function CopyToClipboard (s)
{
if (Window.clipboarddata && clipboarddata.setdata)
{
Clipboarddata.setdata ("Text", s);
}
Else
{
You are have to sign the "code to enable" or allow the action in about:config by changing
User_pref ("Signed.applets.codebase_principal_support", true);
Netscape.security.PrivilegeManager.enablePrivilege (' Universalxpconnect ');

var clip components.classes[' @mozilla. org/widget/clipboard;[ [[[1]]]. CreateInstance (Components.interfaces.nsIClipboard);
if (!clip) return;

Create a transferable
var trans = components.classes[' @mozilla. org/widget/transferable;[ [[[1]]]. CreateInstance (Components.interfaces.nsITransferable);
if (!trans) return;

Specify the data we wish to handle. PlainText in the case.
Trans.adddataflavor (' Text/unicode ');

To get the data from the transferable we need two new objects
var str = new Object ();
var len = new Object ();

var str = components.classes["@mozilla. org/supports-string;[ [[[1]]]. CreateInstance (Components.interfaces.nsISupportsString);

var Copytext=meintext;

Str.data=copytext;

Trans.settransferdata ("Text/unicode", Str,copytext.length*[[[[2]]);

var Clipid=components.interfaces.nsiclipboard;

if (!clip) return false;

Clip.setdata (Trans,null,clipid.kglobalclipboard);
}
}

Use the example code above ...

The code is as follows Copy Code

<textarea id= ' testtext ' > #COPYTOCLIPBOARD code#</textarea><br>
<button onclick= ' CopyToClipboard (document.getElementById (' Testtext '). value); ' >

Permissions

Mozilla-based browsers will request permission to have the user copy the Clipboard.

Note: There are currently no scripts to support safari that can be replicated to the Clipboard

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.