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 ".