Modifying the clipboard event
- Intercept the clipboard event (`copy` event).
- Prevent the default action of copying the content when `Ctrl + C` is pressed.
Syntax:
// Add an event listener to the copy event on the document or specific elements
document.addEventListener('copy', function(event) {
// Prevent the default action of copying the content when Ctrl + C is pressed
event.preventDefault();
});
Example: To demonsrtate disabling the Ctrl+C in JavaScript by modifying the Clipboard event.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p {
font-size: xx-large;
font-weight: bold;
}
</style>
</head>
<body>
<p>This text cannot be copied using Ctrl+C.</p>
<script>
document.addEventListener('copy', function (event) {
event.preventDefault();
alert("Copying is disabled.");
});
</script>
</body>
</html>
Output:
How to Disable Ctrl + C in JavaScript ?
Disabling Ctrl+C in JavaScript involves intercepting the keyboard event and preventing the default action associated with the combination.
There are several approaches to disable Ctrl+C in JavaScript which are as follows:
Table of Content
- Using Event Listeners
- Modifying the clipboard event