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