“Prevents the Gentleman, Not the Villain”: Disabling Browser Access to a Web Page’s HTML Source Code
Sometimes we build a simple HTML website but do not want others to see the page’s source code. In that case, we can use code to prevent others from viewing the HTML code. First, let’s analyze how people view a web page’s source code.
Generally, we know that when you open a web page in a browser, after the page has fully loaded, you can right-click on a blank area of the page and select View Source to see the web page’s source code. Therefore, our approach is to disable users’ ability to right-click on the web page.
Add the following code to the HTML page to disable right-clicking, copying, and selecting.
<script>
document.oncontextmenu=new Function("return false")
document.onselectstart=new Function("return false")
</script>
This way, others will not be able to right-click on the web page. Of course, this simple technique can only stop inexperienced beginners; most programmers can easily bypass the restriction and obtain the code.
Comments
(0)No comments yet. Start the conversation.