通过使用JavaScript屏蔽最常用的四种查看网站源码方法:
- F12
- 鼠标右键单击
- Ctrl+Shift+I
- Ctrl+U
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS禁止查看网页源代码的简单实现方法</title>
</head>
<body>
<script type="text/javascript">
window.onload = function(){
//屏蔽键盘事件
document.onkeydown = function (){
var e = window.event || arguments[];
//F
if(e.keyCode == ){
return false;
//Ctrl+Shift+I
}else if((e.ctrlKey) && (e.shiftKey) && (e.keyCode == )){
return false;
//Shift+F
}else if((e.shiftKey) && (e.keyCode == )){
return false;
//Ctrl+U
}else if((e.ctrlKey) && (e.keyCode == )){
return false;
}
};
//屏蔽鼠标右键
document.oncontextmenu = function (){
return false;
}
}
</script>
</body>
</html>
<script>
document.oncontextmenu = function () {
return false; };
document.onkeydown = function () {
if (window.event && window.event.keyCode == 123) {
event.keyCode = 0;
event.returnValue = false;
return false;
}
};
/** 全局事件监听*/
document.body.onkeydown = function(e){
var keyCode = e.keyCode || e.which || e.charCode;
var ctrlKey = e.ctrlKey || e.metaKey;
if(ctrlKey && (keyCode == 83 || keyCode == 85 || keyCode == 73)) {
e.preventDefault();
return false;
}
else if(ctrlKey && (keyCode == 83 || keyCode == 85 || keyCode == 67)) {
e.preventDefault();
return false;
}else if(keyCode && keyCode == 123){
return false;
}
}
</script>
单独禁止右键+F12
<script>
document.oncontextmenu = function () {
return false; };
document.onkeydown = function () {
if (window.event && window.event.keyCode == 123) {
event.keyCode = 0;
event.returnValue = false;
return false;
}
};
</script>
评论已关闭