JavaScript 网页禁用屏蔽常用操作
前言
小结一下网页前端常用的JavaScript屏蔽操作,包括网络劫持、禁止复制、禁止按键、清除缓存记录等操作。
1、网络劫持
// 网络劫持禁止直接跳转
function network_hijacking() {
var strSourceURL = document.referrer;
var urls = ['192.168.248.128']; //指定要监听的域名或地址,多个可以在数组中直接追加。如:['www.123.com', '123.com', '123']
var result = false;
for (key in urls) {
if (strSourceURL.indexOf(urls[key]) >= 0) {
// 指定的字符串Urls[key]在字符串中出现则>=0,否则为-1
// 置标志
result = true;
// 匹配后跳出循环
break;
}
}
return result;
}
// 登录加载
window.onload = function() {
if(network_hijacking()==false)
{
//按照返回值执行对应操作
window.location.replace("error.html");
return;
}
}
2、禁止复制
//阻止复制
document.body.oncopy = function() {
return false;
}
3、禁止菜单
// 禁止右键菜单
document.oncontextmenu = new Function("event.returnValue=false");
//禁止开始菜单
document.onselectstart = new Function("event.returnValue=false");
4、禁止按键
//按键触发
document.onkeydown = function(){
//禁止ctrl+u
if (event.ctrlKey && window.event.keyCode==85){
return false;
}
//禁止 F12
if (window.event && window.event.keyCode == 123) {
event.keyCode = 0;
event.returnValue = false;
}
//禁止ctrl+s
if (event.ctrlKey && window.event.keyCode==83){
return false;
}
//禁止 F5
if (window.event && window.event.keyCode == 116) {
event.keyCode = 0;
event.returnValue = false;
}
}
5、清除缓存
// 清除缓存记录
window.onload = function() {
//清理缓存
document.getElementById('form').reset();
// 关闭记录
document.getElementById('input').autocomplete = "off";
}