JavaScript prohibit copy and paste implementation code _javascript tips

Source: Internet
Author: User

This operation is a daily user of some basic operations, but some sites in order to protect copyright (such as novel class, Picture Class), prohibit users to perform these operations, so you can prevent users will be browsing the text, by copying, pasting the way to spread.

Oncopy Event:

Definitions and usage

The Oncopy event is triggered when the user copies the contents of the element.

tip: The Oncopy event also triggers when the user copies elements, such as copy elements.

tip: The Oncopy event is usually used to type= the <input> element of "text."

tip: There are three ways to copy elements and content:

Press CTRL + C

In your browser's Edit menu, choose Copy.

Mail mouse button, select Copy from the context menu.

Browser support

Grammar

In HTML:

<element oncopy= "MyScript" >

In javascript:

Object.oncopy = function () {
 //Operation 
  MyScript 
}

In JavaScript, use the AddEventListener () method:

Object.addeventlistener (' Copy ', myScript);
IE8 and earlier IE versions do not support the AddEventListener () method

Onpaste Event:

Definitions and usage

The Onpaste event is triggered when the user pastes text into the element.

Note: Although the HTML element used supports the Onpaste event, it does not actually support all elements, such as the <p> element, unless the contenteditable is set to "true" (see more instances below).

Tip: The Onpaste event is usually used to type= the <input> element of "text."

Tip: There are three ways to paste content in an element:

• Press CTRL + V
• Choose "Paste" from the browser's Edit menu
• Right-click the mouse button to select the "Paste" command in the context menu.

Browser support

Grammar

In HTML:

<element onpaste= "MyScript" >

In javascript:

Object.onpaste = function () {
 //Operation
 myScript; 
}

JavaScript total, using the AddEventListener () method:

Object.addeventlistener (' paste ', myScript);
Internet Explorer 8 and earlier versions of IE do not support the AddEventListener () method.

Implementation principle:

Performs a copy-and-paste event and returns false in the event.

JavaScript code:

var bodymain = document.getElementById (' Bodymain ');
        
         Prohibit copy
         bodymain.oncopy = function () {return
           false;
        }
         Prohibit pasting
         bodymain.onpaste = function () {return
           false;
}

The above JavaScript is forbidden to copy and paste the implementation code is small series to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.