Disable or Preventing Copying of Text Using JavaScript
By JavaScript we can protect our text from copy-pasting, So the users are not able to do the copy of the text.
If we want to disable selection of text to protect our text or content from copy-pasting, then we can use the oncopy event of the element.
Example:
HTML:
<p id="myContent">Hello dear user, Please try to copy this content.</p>
JS:
var myContent = document.getElementById("myContent"); myContent.oncopy = function () { alert("Copying is forbidden!"); return false; };
As you seen in above example, I register the oncopy event on the myContent element.
In this oncopy event we have to return false; to terminate the copying action so the content will be not copy.
When we return false; from the oncopy event than JavaScript will be preventing the copy of the content.
So compulsory we have to return false from the oncopy event.