Home > Web Front-end > JS Tutorial > body text

How to prevent others from viewing website source code in JS

高洛峰
Release: 2016-10-12 15:54:58
Original
1658 people have browsed it

Four viewing paths:

View effect: Poke

1. Press F12 directly

2. Ctrl+Shift+I to view

3. Right-click the mouse to view

4. Ctrl+u=view-source: +url

Just block the above three states. Document has onkeydown (keyboard key event). Just find the corresponding keycode in the event and process it.

Document also has oncontextmenu right mouse button event, just block it. Ctrl+u in 4 can be blocked, [but if you add view-source: in front of the URL, you can still view it after refreshing^_^]

JS code is as follows:

window.onload=function(){
        document.onkeydown=function(){
            var e=window.event||arguments[0];
            if(e.keyCode==123){
                alert("小样你想干嘛?");
                return false;
            }else if((e.ctrlKey)&&(e.shiftKey)&&(e.keyCode==73)){
                alert("还是不给你看。。");
                return false;
            }else if((e.ctrlKey)&&(e.keyCode==85)){//追加

        return false;
       }
        };
        document.oncontextmenu=function(){
            alert("小样不给你看");
            return false;
        }
    }
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template