How to prohibit copying in javascript

WBOY
Release: 2022-04-11 16:37:31
Original
2078 people have browsed it

In JavaScript, you can use the oncopy event to prohibit copying. This event is triggered when the user copies the content on the element. It will be triggered when "CTRL C" is pressed or copy is selected in the browser's edit menu. Event, the syntax is "object.oncopy=function(){...}".

How to prohibit copying in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to prohibit copying in javascript

The oncopy event is triggered when the user copies the content on the element.

Tip: The oncopy event is also triggered when the user copies an element, for example, copying the How to prohibit copying in javascript element.

Tip: The oncopy event is usually used for elements of type="text".

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

  • Press CTRL C

  • in your browser Select "Copy" from the Edit menu

  • Right-click the mouse button and select the "Copy" command from the context menu.

Syntax

In HTML:

<element oncopy="myScript">
Copy after login

In JavaScript:

object.oncopy=function(){myScript};
Copy after login

In JavaScript, use addEventListener () Method:

object.addEventListener("copy", myScript);
Copy after login

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
</head>
<body>
<input type="text" oncopy="myFunction()" value="尝试拷贝文本">
<p id="demo"></p>
<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "你拷贝了文本!"
}
</script>
</body>
</html>
Copy after login

Output result:

How to prohibit copying in javascript

[Related recommendations: javascript video tutorialweb front end

The above is the detailed content of How to prohibit copying in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!