How to Block Right Mouse Button and Block User from Viewing Source Code

Asked

Viewed 7,926 times

3

I wonder if you have to block the right mouse click, with the intention of hiding my source code, but if the user is browsing an android is just typing view-source: and show the code, I’d like to know how to block this command too, if there’s a way to do it

  • 2

    You should make a safe application and not try to hide your code search on Gulp and Grunt besides the F12 tbm shows the code

  • 1

    Don’t worry about it. With time and practice your code gets clean and organized to the point where you’re not afraid of other people seeing.

4 answers

6


IS impossible hide the source code even by disabling the right mouse button for clicks on the page with:

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

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

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

As to the view-source:, this is a browser feature that cannot be disabled via Javascript (or any other script). And it doesn’t even have to be Android: the desktop also works.

  • and encrypt the code ?

  • You do. An interesting site to encrypt HTML is: http://snapbuilder.com/code_snippet_generator/obfuscate_html_source_code/

  • But use only to encrypt HTML, do not use to encrypt scripts. To encrypt scripts, use other websites that do this separately.

  • actually I wanted to encrypt my CSS

  • If the CSS is not external, the site above works. Otherwise, I don’t know how to inform.

4

It is not advisable to put this there ...

document.oncontextmenu = document.body.oncontextmenu = function() {return false;}
  • upvote responds a part that I did not answer, I will add in my.

  • 1

    Upvote, you answered a part I did not answer, about blocking the right click. I just answered about blocking view the html page.

3

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

About Lock command to see Source Code

It is not possible to prevent the user from entering the code (view-source:) running on the machine. After all, the HTML they will receive will be readable in plain text. You can cause a nuisance for most people, but that won’t be a valid security measure - the chrome extensions will still run, for example, so if someone is using the Noscript extension, they will disable all javascript.

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

There are some free javascript obfuscators, such as https://javascriptobfuscator.com/. Remember that it is not a safe method, however.

Ja Right click can be locked

As told by Edson

Thus:

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

-1

This is a very simple thing to solve. Just type this into your code line (<script/>):

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

Note: this is not recommended on your website.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.