JS webpage click Copy code Summary

Source: Internet
Author: User

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

Function oCopy (obj ){
Obj. select ();
Js = obj. createTextRange ();
Js.exe cCommand ("Copy ")

<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 ");

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. ");
<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)
Var trans = Components. classes ["@ mozilla.org/widget/transferable1_1"]. createInstance (Components. interfaces. nsITransferable );
If (! Trans)
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 );


Related Article

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.