Copy data to the Clipboard using JavaScript in Firefox (Copy to Clipboard cross-browser Edition)

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta name = "Keywords" content = "YES! B/S! "/>
<Meta name = "Description" content = "This page is 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 you from performing clipboard operations. Open 'about: config' and set signed. applets. codebase_principal_support 'to true and then try again ");
Return false;
}
Var clip = Components. classes ['@ mozilla.org/widget/clipboard=1'mirror.createinstance (Components. interfaces. nsIClipboard );
If (! Clip)
Return;
Var trans = Components. classes ['@ mozilla.org/widget/transferable00001').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-string1_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>
</Head>
<Body>
<Button onclick = "test ();"> copy to clipboard </button>
</Body>
</Html>

Note: Firefox's default security restrictions cannot be copied to the Clipboard through JavaScript. You must be prompted to modify Firefox settings. Open "about: config" and 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.