[JavaScript]JS遮蔽瀏覽器右鍵選單/貼上/複製/剪下/選中 [轉載]
前兩天在解決一個專案缺陷時,突發感興趣,瞭解一下~
1 JS事件
document.oncontextmenu // 右鍵選單
document.onpaste //貼上
document.oncopy //複製
document.oncut //剪下
document.onselectstart //選中
2 JS原始碼
//遮蔽右鍵選單 (<input> / <textarea> / type=text 除外) document.oncontextmenu = function (event){ if(window.event){ event = window.event; }try{ var the = event.srcElement; if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){ return false; } return true; }catch (e){ return false; } } //遮蔽貼上 (<input> / <textarea> / type=text 除外) document.onpaste = function (event){ if(window.event){ event = window.event; }try{ var the = event.srcElement; if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){ return false; // false 表示 禁用 貼上功能 } return true; }catch (e){ return false; } } //遮蔽複製 (<input> / <textarea> / type=text 除外) document.oncopy = function (event){ if(window.event){ event = window.event; }try{ var the = event.srcElement; if(!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){ return false; } return true; }catch (e){ return false; } } //遮蔽剪下 (<input> / <textarea> / type=text 除外) document.oncut = function (event){ if(window.event){ event = window.event; }try{ var the = event.srcElement; if(!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){ return false; } return true; }catch (e){ return false; } } //遮蔽(文字)選中 (<input> / <textarea> / type=text 除外) document.onselectstart = function (event){ if(window.event){ event = window.event; }try{ var the = event.srcElement; if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){ return false; } return true; } catch (e) { return false; } }
3 附件: 完整原始碼
展開檢視
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js遮蔽瀏覽器右鍵選單,貼上,複製,剪下,選中</title>
<meta name="description" content="js程式碼製作遮蔽瀏覽器右鍵選單,遮蔽瀏覽器貼上,遮蔽瀏覽器複製,遮蔽瀏覽器剪下,遮蔽瀏覽器選中,不會遮蔽搜尋引擎蜘蛛抓取頁面,不影響seo優化。可以設定網頁內容程式碼不被使用者隨意下載等。" />
</head><body>
<input type="input" />
<script type="text/javascript">
//遮蔽右鍵選單 (<input> / <textarea> / type=text 除外)
document.oncontextmenu = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
}catch (e){
return false;
}
}//遮蔽貼上 (<input> / <textarea> / type=text 除外)
document.onpaste = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false; // false 表示 禁用 貼上功能
}
return true;
}catch (e){
return false;
}
}//遮蔽複製 (<input> / <textarea> / type=text 除外)
document.oncopy = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if(!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
}catch (e){
return false;
}
}//遮蔽剪下 (<input> / <textarea> / type=text 除外)
document.oncut = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if(!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
}catch (e){
return false;
}
}//遮蔽(文字)選中 (<input> / <textarea> / type=text 除外)
document.onselectstart = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
} catch (e) {
return false;
}
}
</script><div style="width:600px;margin:40px auto;text-align:center;border:solid 1px #FFCA52;background:#FFFDD2;height:28px;line-height:28px;font-size:14px;padding:5px 10px;color:#ff0000;font-weight:800;">單頁禁用右鍵選單、貼上、複製、剪下、選中操作</div>
test
</body>
</html>