In js, if you want to achieve the simplest copy content, you can click the button to copy the content, we will use window. clipboardData. setData (\ "Text \", \ 'contains www. bKjia. c0m \ '); below I will summarize various replication effect codes.
Code 1: copy the topic address and send it to a friend on QQ/MSN.
| The Code is as follows: |
Copy code |
|
<Input type = "button" name = "Submit" onClick = 'copytoclipboard () 'value = "copy the topic address and send it to friends on QQ/MSN">
<Script language = "javascript">
Function copyToClipBoard (){
Var clipBoardContent = "";
ClipBoardContent + = document. title;
ClipBoardContent + = "";
ClipBoardContent + = this. location. href;
Window. clipboardData. setData ("Text", clipBoardContent );
Alert ("Copied successfully, paste it on your QQ/MSN and recommend it to your friends ");
}
</Script> |
Copy the code of the text box:
| The Code is as follows: |
Copy code |
|
<Script>
Function oCopy (obj ){
Obj. select ();
Js = obj. createTextRange ();
Js.exe cCommand ("Copy ")
}
</Script>
<Input onclick = "oCopy (this)" value = "content to copy! "> |
The connection is automatically added when the user copies the article.
A comprehensive copy of code that can be judged by different browsers (IE/Firefox/NS)
| The Code is as follows: |
Copy code |
|
/**
* JS copy code
*/
Function copyData (text ){
Var clipBoardContent, clip, trans, str, len, str, copytext, clipid;
ClipBoardContent = text;
If (window. clipboardData ){
Window. clipboardData. clearData ();
Window. clipboardData. setData ("Text", clipBoardContent );
}
Else if (navigator. userAgent. indexOf ("Opera ")! =-1 ){
Window. location = clipBoardContent;
}
Else if (window. netscape ){
Try {
Netscape. security. PrivilegeManager. enablePrivilege ("UniversalXPConnect ");
} Catch (e ){
Alert ("your current browser settings have disabled this function! Follow these steps to enable this function! N open a new browser, enter 'about: config' in the address bar of the browser, and press Enter. N, find the 'signed. applets. codebase_principal_support 'item, double-click it, and set it to 'true '. N statement: this function will not endanger the security of your computer or data! ");
}
Clip = Components. classes ['@ mozilla.org/widget/clipboard=1'mirror.createinstance (Components. interfaces. nsIClipboard );
If (! Clip) return;
Trans = Components. classes ['@ mozilla.org/widget/transferable00001'hangzhou.createinstance (Components. interfaces. nsITransferable );
If (! Trans) return;
Trans. addDataFlavor ('text/unicode ');
Str = new Object ();
Len = new Object ();
Str = Components. classes ["@ mozilla.org/supports-string1_1"]. createInstance (Components. interfaces. nsISupportsString );
Copytext = clipBoardContent;
Str. data = copytext;
Trans. setTransferData ("text/unicode", str, copytext. length * 2 );
Clipid = Components. interfaces. nsIClipboard;
If (! Clip) return false;
Clip. setData (trans, null, clipid. kGlobalClipboard );
}
Return true;
} |
<Title> copying prohibited by JS </title>
<Body> This is a piece of test text. You can right-click to test: <br> we provide you with high-quality programming source code, JavaScript/CSS special effect code, and common programming software download. </Body>
| The Code is as follows: |
Copy code |
|
<Script> document. body. onselectstart = document. body. oncontextmenu = function () {return false ;}; </script> |