Sometimes we build a simple HTML website but don't want others to see the webpage's source code. In that case, we need to use code to prevent others from viewing the HTML code. Let's first analyze how others view webpage source code.
Generally, we all know that when you open a webpage in a browser, after the page has fully loaded, you can right-click in a blank area of the webpage and select View Source to view the webpage's source code. So our approach is to prevent users from right-clicking on the webpage.
Add the following code to the HTML webpage to disable the right-click, copy, and selection functions.
<script>
document.oncontextmenu=new Function("return false")
document.onselectstart=new Function("return false")
</script>
This way, others will be unable to right-click on the webpage. Of course, this simple trick can only stop beginners; most programmers can easily bypass the restriction and obtain the code.