Simple implementation of JS forbidding to view web source code

Keywords: Javascript

In project development, we sometimes encounter the source code information of the website that we don't want others to easily view. We have many ways to lightly protect our own website source code. Here are three ways to use JavaScript to protect your website source code:

The most common way to check the source code of a website is to have these four kinds:

2. Right click
The above three methods can view the source code of the website. We can use JavaScript to block these three states to achieve the effect of forbidding viewing the source code. Put the source code directly below.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>JS A simple way to disable viewing the source code of web page</title>
    <script type="text/javascript">
        window.onload = function(){
            //Block keyboard events
            document.onkeydown = function (){
                var e = window.event || arguments[0];
                if(e.keyCode == 123){
                    return false;
                }else if((e.ctrlKey) && (e.shiftKey) && (e.keyCode == 73)){
                    return false;
                }else if((e.shiftKey) && (e.keyCode == 121)){
                    return false;
                }else if((e.ctrlKey) && (e.keyCode == 85)){
                    return false;
            //Mask right mouse button
            document.oncontextmenu = function (){
                return false;


It's impossible to realize the pure. html of source code shielding. There's no way to stop it if you want to see the source code. This script can only prevent the little white gained without any effort. It can't work for the old computer and the big God. And now many browsers have the function of viewing the source code of the web page. You can download any file with the TELEPROT website download tool, which is equivalent to a mirror website. Want to really protect the source code, unless the server security settings, encryption.

Thanks for sharing:

Posted by 9999 on Tue, 07 Jan 2020 02:29:30 -0800