Mouse Right Button Click, CTRL +U disabling in HTML to protect content from Copying and Viewing source by visitor
In some rare cases, we need to protect our source code form visitors. Sometimes, for security reasons, to safeguard source code from hackers.
Disabling is easy.
It can be done in three steps:
Warning: If add these attributes in body tag then, right click will work where the body tag is finished [in such case, where the page body doesn't feel full display of the browser]
Full code can be found in my Gist DisableRightClick.html
Disabling is easy.
It can be done in three steps:
- Add the attribute to any HTML Tags
- Add JavaScript code to disable some KEYS with the onkeydown event
- Add JQuery to prevent a combined key event.
<html lang="en" oncontextmenu="return false" ondragstart="return false" onselectstart="return false">
Warning: If add these attributes in body tag then, right click will work where the body tag is finished [in such case, where the page body doesn't feel full display of the browser]
JavaScript Code
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117)) {
alert('You are not allowed to perform this action.');
return false;
} else {
return true;
}
};
</script>
jQuery Code
Before Adding the following code, we must call jQuery.js
<script>
$(document).bind('keydown', function(e) {
if(e.ctrlKey && (e.which == 83)) {
e.preventDefault();
alert('You cannot save this page');
return false;
}
});
</script>
Full code can be found in my Gist DisableRightClick.html
Comments
Post a Comment