The simplest way to copy content in js is to directly use createTextRange () and execCommand () and then there are window. clipboardData. setData and so on. Next I will summarize various application instances.
Example 1
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! "> |
Example 2
Click to copy the title and address code:
The Code is as follows: |
Copy code |
<Title> title </title> <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> |
3. Click the code to copy the text box content: <br>
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Function jsCopy (){ Var e = document. getElementById ("content"); // The object is content E. select (); // select an object Document.exe cCommand ("Copy"); // execute the browser Copy command Alert ("copied, can be pasted. "); } </Script> <Textarea cols = "60" name = "content" rows = "2" id = "content"> www.52nx.net </textarea> <Input type = button value = "copy" onclick = "jsCopy ()"> |
The above compatibility may be different in different browsers. I will introduce a very good example below.
The Code is as follows: |
Copy code |
Function copy_clip (){ Var url = $ ("# back_info" ).html (); // the content to be copied Var txt = url. substring (url. indexOf (":") + 1, url. length ); If (window. clipboardData ){ Window. clipboardData. clearData (); Window. clipboardData. setData ("Text", txt ); Alert ('Congratulations, copy successful! '); } 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 to clipboard operations. Please enter 'about: config' in the address bar of the new window and find 'signed. applets. codebase_principal_support 'is set to true '"); Return false; } Var clip = Components. classes ["@ mozilla.org/widget/clipboard1_1"]. createInstance (Components. interfaces. nsIClipboard ); If (! Clip) Return; Var trans = Components. classes ["@ mozilla.org/widget/transferable1_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-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 );
} }
|