Home > Article > Web Front-end > How to prevent others from viewing website source code in JS
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; } }