Application Instance of the clipboardData object

Source: Internet
Author: User

 

Note: IE7 and IE8 have the copy permission on the webpage. You must set it in the "Custom Level" script in "security ".

ClipboardData object

Provides access to the clipboard.

Three methods
1. cleardata (sdataformat) deletes data in the specified format on the clipboard.
2. getdata (sdataformat) obtains data in the specified format from the clipboard.
3. setdata (sdataformat, sdata) assigns data in the specified format to the clipboard. True indicates that the operation is successful.

Example

<Script language = "JavaScript">
<! --
VaR text = "123 ";
If (! Window. clipboardData. setdata ('text', text) // assign data in text format
{
Alert ("copying failed! ");
}
TEXT = Window. clipboardData. getdata ('text'); // obtain text data
Alert (text );
Window. clipboardData. cleardata ('text'); // clear text data
TEXT = Window. clipboardData. getdata ('text ');
Alert (text );
// -->
</SCRIPT>

Some methods:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> JS copy Code </title>
</Head>
<Body>
<P>
<Input type = "button" name = "anniu1" onclick = 'copytoclipboard () 'value = "copy the topic address and URL address to your 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>
<Br/>
<Br/>

Copy URL directly
<Input type = "button" name = "anniu2" onclick = 'copyurl () 'value = "copy URL address">
<Script language = "JavaScript">
Function copyurl ()
{
VaR clipboardcontent = This. Location. href;
Window. clipboardData. setdata ("text", clipboardcontent );
Alert ("Copied successfully! ");
}
</SCRIPT>
<Br/>
<Br/>
When you click the text box, copy the content in the text box
<Input onclick = "ocopy (this)" value = "Hello. content to copy! ">
<Script language = "JavaScript">
Function ocopy (OBJ ){
OBJ. Select ();
JS = obj. createTextRange ();
Js.exe ccommand ("copy ")
Alert ("Copied successfully! ");
}
</SCRIPT>
<Br/>
<Br/>
Copy the text box or hide the content in the field
<Script language = "JavaScript">
Function copyurl (target ){
Target. value = myimg. value;
Target. Select ();
JS = myimg. createTextRange ();
Js.exe ccommand ("copy ");
Alert ("Copied successfully! ");
}
Function addimg (target ){
Target. value = "[img]" + myimg. Value + "[/img]";
Target. Select ();
JS = target. createTextRange ();
Js.exe ccommand ("copy ");
Alert ("Copied successfully! ");
}
</SCRIPT>

<Input name = myimg type = hidden id = myimg value = "http://pmp.cnblogs.com"/>
<Input name = imgurl type = text size = 32 value = "http://pmp.cnblogs.com"/>
<Input type = button value = "Click here to copy the address of this site" onclick = "copyurl (imgurl);"/>

<Br/>
<Br/>
Copy the content in the span tag
<SCRIPT type = "text/JavaScript">
</SCRIPT>
<Br/>
<Br/>
<SCRIPT type = "text/JavaScript"> function copytext (OBJ)
{
VaR RNG = Document. Body. createTextRange ();
RNG. movetoelementtext (OBJ );
RNG. scrollintoview ();
RNG. Select ();
Rng.exe ccommand ("copy ");
RNG. Collapse (false );
Alert ("Copied successfully! ");
}
</SCRIPT>

The following is a code snippet: <br/>
<Br/>
<Span id = "tbid"> http://pmp.cnblogs.com </span>
[<A href = "#" onclick = "copytext (document. All. tbid)"> click to copy </a>] <br/>
<Span id = "tbid2"> http://www.cnblogs.com/pmp </span>
[<A href = "#" onclick = "copytext (document. All. tbid2)"> click to copy </a>] <br/>

</P>
</Body>
</Html>

 

 

Another method is as follows:

Function copyqq (qq ){
VaR OBJ = Document. getelementbyid (qq );
OBJ. Select ();
JS = obj. createTextRange ();
Js.exe ccommand ("copy ");
Alert ("the code has been successfully copied! ");

}

 

 

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.