You may want to prevent others from inspecting your website, view source codes, or confidential functions to secure your website or prevent others from duplication your website. And it matters a lot when you spend a lot of time and hard work.
So, in this article, we won’t disable the right click but also all those shortcuts that allow a person to inspect your website.
Disclaimer: Inspect element will only be disabled on those pages/posts where the codes are present. That’s why we recommend you to implement the codes inside your header or somewhere that appears on all pages/posts. Try to avoid using the codes inside your Footer because codes in footers will load after all other contents are loaded, and until the load of the codes, the inspect element won’t be disabled.
So, let’s jump inside!
Step 1 – Disabling Mouse Right Click On Website
To disable mouse right-click, add these codes in your site inside the ‘<script></script>‘ tag:
document.addEventListener('contextmenu', function(e){ e.preventDefault(); });
Additionally, you can show an alert when your visitors click the mouse right button. To do so, you can use this instead of that code(the same code but alert code inside it):
document.addEventListener('contextmenu', function(e){ alert("Sorry, right click is disabled to prevent leakage of confidential functions"); e.preventDefault(); });
Now follow the next step.
Step 2 – Disabling Shortcut key combination that allows inspecting elements
There are few shortcuts available to use the ‘inspect element‘ or ‘view page-source‘ option. Such As:
- “Ctrl+Shift+I” for ‘Inspect Element‘
- “Ctrl+Shift+J” for ‘Inspect Element‘ Console panel
- “Ctrl+Shift+C” for ‘Inspect Element‘ Elements panel
- “Ctrl+S” for Saving Website as HTML file
- “Ctrl+U” for ‘View Source Code‘
So, now we will disable these shortcut keys. To do so, add these codes after the codes of step 2 (but before the end script tag ‘</script>‘):
document.onkeydown = function(e) { if(event.keyCode == 123) { return false; } if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) { return false; } if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) { return false; } if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) { return false; } if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)) { return false; } if(e.ctrlKey && e.keyCode == 'S'.charCodeAt(0)) { return false; } }
Congratulations!
You have disabled the ‘Inspect Element’ And ‘View Page Source’ Option from your website.
Thanks For Allowing Us To Help You 🙂
If you are confused or want to know something, then let us know in the comment box, we will reach you as soon as possible. Don’t Forget To Subscribe our Newsletter, YouTube Channel, and Like Our Facebook Page To Keep Updated With Awesome Things. Follow us on Twitter to stay updated with the latest news & changes.