How to lock right mouse button and Block user from viewing Source Code

Posted on

Question :

I would like to know if you can block the right click of the mouse, with the intention of hiding my source code, but if the user is browsing an android, just type view-source: and show the code, I would like to know how to block this command too, if it has a way for it


Answer :

It is impossible to hide the source code, even disabling the right mouse button to click the page with:

document.addEventListener('contextmenu', event => event.preventDefault());

Just type CTRL + U and Voilà : source code on the screen. Not to mention that some browsers have ALT menus with the option to view source code.

Using this feature makes it very difficult to access source code for lay users, however, lay users are not supposed to be interested in seeing your source code, and even if they were, it would be enough for a simple Google search that would find it quite easy to do that.

As for view-source: , this is a browser feature that can not be disabled via JavaScript (or any other script). And it does not even have to be Android: the desktop also works.


It is not recommended to have this here …

document.oncontextmenu = document.body.oncontextmenu = function() {return false;}


Emphasizing that all two are bad practices, follow the answers:

About blocking the command to see the source code

It is not possible to prevent the user from inspecting the code (view-source 🙂 running on the machine. After all, the HTML they will receive will be readable in plain text. You may cause a nuisance to most people, but this will not be a valid security measure – chrome extensions will still be run, for example, so if someone is using the NoScript extension, they will disable all javascript.

A much better option would be to deal with your logic server, and just send the client the information they need to know / request.

There are some free javascript obfuscators, such as link . Remember that it is not a safe method, though.

Ja Right click can be blocked

As told by Edson


document.oncontextmenu = document.body.oncontextmenu = function() {return false;}


Leave a Reply

Your email address will not be published. Required fields are marked *