这篇文章主要讲解:右键复制失效方法、菜单-文件-另存为-失效方法、防止查看源代码进行复制的方法、防止页面缓存的方法。来达到一定的代码保护效果
右键复制失效方法:
<script language="Javascript">
document.oncontextmenu=new Function("event.returnValue=false");document.onselectstart=new Function("event.returnValue=false");
</script>
<body oncontextmenu="return false" onselectstart="return false">
<body oncontextmenu="event.returnValue=false" onselectstart="event.returnValue=false">
<body oncopy="alert('对不起,本网页禁止复制!');return false;">
body{//通过css设置body
-webkit-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
}
菜单"文件"-"另存为"失效方法:
<noscript><iframe src="*.htm"></iframe></noscript>
防止查看源代码进行复制的方法:
<script language="javascript">
function click(e) {
if(document.all) {
if(event.button == 2 || event.button == 3) {
oncontextmenu = 'return false';
}
}
if(document.layers) {
if(e.which == 3) {
oncontextmenu = 'return false';
}
}
}
if(document.layers) {
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown = click;
document.oncontextmenu = new Function("return false;")
document.onkeydown = document.onkeyup = document.onkeypress = function() {
//123屏蔽F12,73屏蔽Ctrl+Shift+I,121屏蔽Shift+F10
if(window.event.keyCode == 123 || window.event.keyCode == 73 || window.event.keyCode == 121) {
window.event.returnValue = false;
return(false);
}
}
</script>
防止页面缓存的方法
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
<img src="picture.jpg?1222259157.415" alt="">
<script>
document.write("<script type='text/javascript' src='test.js?"+Math.random();+"'></script>");
</script>
总结: