Firefox to copy data to the Clipboard via JavaScript (copy to Clipboard Cross browser version) _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Keywords" content= "yes! B/s! "/>
<meta name= "Description" content= "This page was from http://Justinyoung.cnblogs.com"/>
<title>css/javascript demo</title>
<script type= "Text/javascript" language= "JavaScript" >
function Test () {
CopyToClipboard ("dddd");
}
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);
}
}
</script>
<body>
<button onclick= "Test ();" > Copy to Pasteboard </button>
</body>

Description: Firefox's default security restrictions do not allow JavaScript to be copied to the Clipboard, you need to prompt the user to modify Firefox settings. The modification method is to open "About:config" to set "Signed.applets.codebase_principal_support" to "true".

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.