Js copy and paste events to obtain clipboard data (not compatible, only webkit is supported

Source: Internet
Author: User

A js copy and paste event demo, which can be viewed here:


<Html>
<Head>
<Title> JS copy and paste events </title>
</Head>
<Body>
<Input type = "text" id = "text"/>
<Input type = "button" id = "btn" value = "copy"/>
<P> Copy and paste data in the clipboard on the console. </p>
</Body>
<Script type = "text/javascript">
// Obtain and paste data
Document. getElementById ('text'). onpaste = function (e ){
E. preventDefault (); // you can disable default pasting.
Var text = (e. originalEvent | e). clipboardData. getData ('text/plain ');
Console. log (text );
  }
// Obtain the copied data
Document. getElementById ('text'). oncopy = function (){
Var text;
If (window. getSelection ){
Text = window. getSelection (). toString ();
} Else if (document. selection & document. selection. createRange ){
Text = document. selection. createRange (). text;
   }
Console. log (text );
Alert ('copied successfully, paste it to the desired location ');
  }
// Click the Copy button event to search for ZeroClipboard for better compatibility and copy the event using swf.
Document. getElementById ('btn '). onclick = function (){
Document. getElementById ('text'). select ();
Alert ('press CTRL + C or right-click to copy ');
  }
</Script>
</Html>


The code for all pages is as follows:

<Html>
<Head>
<Title> JS copy and paste events </title>
</Head>
<Body>
<Input type = "text" id = "text"/>
<Input type = "button" id = "btn" value = "copy"/>
</Body>
<Script type = "text/javascript">
// Obtain and paste data
Document. getElementById ('text'). onpaste = function (e ){
E. preventDefault (); // you can disable default pasting.
Var text = (e. originalEvent | e). clipboardData. getData ('text/plain ');
Console. log (text );
  }
// Obtain the copied data
Document. getElementById ('text'). oncopy = function (){
Var text;
If (window. getSelection ){
Text = window. getSelection (). toString ();
} Else if (document. selection & document. selection. createRange ){
Text = document. selection. createRange (). text;
   }
Console. log (text );
Alert ('copied successfully, paste it to the desired location ');
  }
// Click the Copy button event to search for ZeroClipboard for better compatibility and copy the event using swf.
Document. getElementById ('btn '). onclick = function (){
Document. getElementById ('text'). select ();
Alert ('press CTRL + C or right-click to copy ');
  }
</Script>
</Html>

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.