This operation is a basic daily operation for netizens, but in order to protect copyright (such as novels and pictures), some websites prohibit users from performing these operations, so that users can prevent the text being browsed by copying and pasting.
oncopy event:
Definition and usage
The oncopy event is fired when the user copies the content on the element.
Tip: The oncopy event will also be triggered when the user copies an element, for example, copying the <img> element.
Tip: The oncopy event is usually used in the <input> element of type="text".
Tip: There are three ways to copy elements and content:
Press CTRL + C
Select "Copy" from the Edit menu in your browser
Mail mouse button, select the "Copy" command in the context menu.
Browser support
grammar
In HTML:
<element oncopy="myScript">
In JavaScript:
object.oncopy = function(){ //Operate myScript }In JavaScript, use the addEventListener() method:
object.addEventListener('copy',myScript);//IE8 and earlier IE versions do not support the addEventListener() methodonpaste event:
Definition and usage
The onpaste event is fired when the user pastes text into the element.
Note: Although the HTML elements used support the onpaste event, not all elements are actually supported, such as the <p> element, unless the contenteditable is set to "true" (see more examples below).
Tip: The onpaste event is usually used for the <input> element of type="text".
Tip: There are three ways to paste content in elements:
•Press CTRL + V
•Select "Paste" from the browser's edit menu
• Right-click the mouse button and select the "Paste" command in the context menu.
Browser support
grammarIn HTML:
<element onpaste="myScript">
In JavaScript:
object.onpaste = function(){ //Operate myScript; }Total JavaScript, use the addEventListener() method:
object.addEventListener('paste',myScript);//Internet Explorer 8 and earlier does not support the addEventListener() method.Implementation principle:
Perform a copy and paste event and return false in the event.
JavaScript code:
var bodyMain = document.getElementById('bodyMain' ); //Copying bodyMain.oncopy = function(){ return false; } //Pasting bodyMain.onpaste = function(){ return false;}The above implementation code for prohibiting copying and pasting of JavaScript is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.