As a website owner, one of the main challenges faced is protecting content from unauthorized copy-pasting. Preventive measures are crucial to maintain the integrity and uniqueness of the information we share.
Why should we prevent copy-pasting? Besides safeguarding intellectual property rights, this action can have a serious impact on the reputation and search engine rankings of our website.
Technological advancements have made copy-pasting increasingly easy, emphasizing the need for effective preventive measures. For WordPress users or users of other CMS platforms, preventing copy-paste can be achieved by installing the WP Content Copy Protection plugin.
Meanwhile, for HTML developers, copy-paste prevention can be implemented through a combination of CSS and JavaScript. CSS is used to prevent text from being selected, while JavaScript is employed to inhibit users from copying or right-clicking on the website.
Here is the CSS code to prevent users from making selections:
body {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
And here is the JavaScript code to prevent users from copying and opening the context menu (right-click) on the website:
var addHandler = function (element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent("on" + type, handler);
} else {
element["on" + type] = handler;
}
};
var preventDefault = function (event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
};
addHandler(window, "contextmenu", function (event) {
preventDefault(event);
});
document.onkeydown = function (event) {
if(event.ctrlKey || event.shiftKey || event.keyCode == 123) {
return false;
}
};