JavaScript-compatible IE&FF copy to clipboard

Source: Internet
Author: User
Tags rar

Method One, all JS copied to the clipping version

Can be compatible with basically all of the mainstream browsers (IE,FF), a lot of places to use:

The code is as follows Copy Code
CopyToClipboard = function (TXT) {
if (window.clipboarddata) {
Window.clipboardData.clearData ();
Window.clipboardData.setData ("Text", txt);
else if (navigator.userAgent.indexOf ("Opera")!=-1) {
window.location = txt;
else if (Window.netscape) {
try {
Netscape.security.PrivilegeManager.enablePrivilege ("Universalxpconnect");
catch (e) {
Alert ("Your Firefox security restrictions restrict your clipboard operations, please open ' about:config ' to Signed.applets.codebase_principal_support ' set to True ')";
return false;
}
var clip = components.classes[' @mozilla. Org/widget/clipboard;1′].createinstance ( Components.interfaces.nsIClipboard);
if (!clip)
Return
var trans = components.classes[' @mozilla. Org/widget/transferable;1′].createinstance ( Components.interfaces.nsITransferable);
if (!trans)
Return
Trans.adddataflavor (' Text/unicode ');
var str = new Object ();
var len = new Object ();
var str = components.classes["@mozilla. Org/supports-string;1″].createinstance ( Components.interfaces.nsISupportsString);
var copytext = txt;
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);
}
}

Method Two, Jquery.zclip.js simple implementation Copy to clipboard function demo as follows

The code is as follows Copy Code
<! DOCTYPE html>
<title>zeroclipboard test</title>
<meta charset= "Utf-8" >
<style type= "Text/css" >
. line{margin-bottom:20px;}
/* Copy Prompt * *
. copy-tips{position:fixed;z-index:999;bottom:50%;left:50%;margin:0 0-20px-80px;background-color:rgba (0, 0, 0, 0.2); Filter:progid:DXImageTransform.Microsoft.Gradient (startcolorstr=# 30000000, endcolorstr= #30000000);p adding:6px;
. copy-tips-wrap{padding:10px 20px;text-align:center;border:1px solid #F4D9A6; Background-color: #FFFDEE; font-size : 14px;}
</style>
<script type= "Text/javascript" src= "Jquery.js" ></script>
<script type= "Text/javascript" src= "Jquery.zclip.js" ></script>
<body>
<div class= "line" >
&LT;H2&GT;DEMO1 Click Copy current text <a href= "#none" class= "copy" > click Copy Me </a>
</div>
<div class= "line" >
<a href= "#none" class= "Copy-input" > click the text in the copy </a>
<input type= "text" class= "input" value= "enter the content to be copied"/>
</div>
</body>
<script type= "Text/javascript" >
$ (document). Ready (function () {
/* Define all Class as copy label, click to copy the text of the clicked object.
$ (". Copy"). Zclip ({
Path: "Zeroclipboard.swf",
Copy:function () {
return $ (this). text ();
},
Beforecopy:function () {/* operation while holding down the mouse * *
$ (this). CSS ("Color", "orange");
},
Aftercopy:function () {/* The operation after successful copy * *
var $copysuc = $ ("<div class= ' copy-tips ' ><div class= ' copy-tips-wrap '" >☺ copy Success </div></div> ");
$ (' body '). Find (". Copy-tips"). Remove (). End (). append ($COPYSUC);
$ (". Copy-tips"). Fadeout (3000);
}
});
/* Define all class as Copy-input tags, click to copy the class as input text * *
$ (". Copy-input"). Zclip ({
Path: "Zeroclipboard.swf",
Copy:function () {
Return $ (a). Parent (). Find (". Input"). Val ();
},
Aftercopy:function () {/* The operation after successful copy * *
var $copysuc = $ ("<div class= ' copy-tips ' ><div class= ' copy-tips-wrap '" >☺ copy Success </div></div> ");
$ (' body '). Find (". Copy-tips"). Remove (). End (). append ($COPYSUC);
$ (". Copy-tips"). Fadeout (3000);
}
});
});
</script>

Method Three, concise version and method a little bit like oh but the code is simple

The code is as follows Copy Code

<script>
Copytoclip (CPSTR) {
if (clipboarddata) {
Clipboarddata.setdata ("Text", Cpstr);}
else if (Netscape) {
Netscape.security.PrivilegeManager.enablePrivilege (' Universalxpconnect ');
var clip = components.classes[' @mozilla. Org/widget/clipboard;1 '].createinstance ( Components.interfaces.nsIClipboard);
var trans = components.classes[' @mozilla. Org/widget/transferable;1 '].createinstance ( Components.interfaces.nsITransferable);
if (!clip | |!trans) return;
Trans.adddataflavor (' Text/unicode ');
var len = new Object ();
var str = components.classes["@mozilla. Org/supports-string;1"].createinstance ( Components.interfaces.nsISupportsString);
var copytext=cpstr;
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);
}
Alert ("Www.111cn.net prompts you to copy" +cpstr)
return false;
}
</script>

Method four, using a plug-in with flash effect

The principle is: Create a hidden Flash file, and give the Flash variable flashvars assignment "clipboard= ...", through this assignment flash will put the copied content to the Clipboard. This method is compatible with IE, Firefox, Opera, Chrome, Safari, and is really a "panacea" solution. The installation rate of the browser flash is very high, which is almost a perfect solution.

JS section:

The code is as follows Copy Code
<script type= "Text/javascript" >
var clipboardswfdata;
var setcopy_gettext = function () {
Clipboardswfdata = document.getElementById (' Test_text '). Value;
alert (clipboardswfdata);
Window.document.clipboardswf.SetVariable (' str ', clipboardswfdata);
}
var Floatwin = function () {
Alert (' Copy successful! ');
document.getElementById (' Clipinner '). style.display = ' None ';
}
</script>

HTML section:

  code is as follows copy code
<textarea id=" Test_text "rows=" "cols=" "> Text? Hot? 11cn.net</textarea
<div id= "clipboard_content" >
  <div class= "My_clip_button" ><span class= "Clipinner" Id= "Clipinner" "?? To cut the book
    <embed name= "clipboardswf" class= "clipboardswf" id= "clipboardswf" onmouseover= " Setcopy_gettext () "devicefont=" false "src="./_clipboard.swf "menu=" false "allowscriptaccess=" Samedomain " Swliveconnect= "true" wmode= "Transparent" type= "Application/x-shockwave-flash" height= "width=" "
    </span>
  </div>
</div>

clipboardswf Download Address: Http://file.111cn.net/upload/2013/12/_clipboard.rar.rar

Related Article

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.