How To Disable 'Inspect Element' And 'View Page Source' Option On Website

How To Disable ‘Inspect Element’ And ‘View Page Source’ Option On Website

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.

Was this helpful?

Mahedi Zaman Zaber

I'm a Driven and ambitious individual who is currently pursuing a Bachelor of Science in Computer Science and Engineering. With a passion for innovation and a desire to make a lasting impact, I aspire to become a distinguished software engineer and entrepreneur.

More Reading

Post navigation

ZealTyro Blog
Get our web app. It won't take up space on your phone.
Install
See this post in...
ZealTyro Blog
Chrome
Add ZealTyro Blog to Home Screen
Close

For an optimized experience on mobile, add ZealTyro Blog shortcut to your mobile device's home screen

1) Press the share button on your browser's menu bar
2) Press 'Add to Home Screen'.
ZealTyro Blog We would like to show you notifications for the latest news and updates.
Dismiss
Allow Notifications