js 屏蔽瀏覽器右鍵菜單


<script type="text/javascript">  
    function doNothing(){  
        window.event.returnValue=false;  
        return false;  
    }  
</script>  
   <body oncontextmenu="doNothing()">  

有時候我們在某些網站上不想用戶點擊右鍵進行復制等操作

在body里面處理下就好了

 

移動端長按會復制等選項可以使用下述的代碼屏蔽這個功能,將下述的css加到代碼中即可

/*在手機瀏覽器中,長按可選中文本,但如果在應用中,會給人一種異樣的感覺,最好還是禁用此功能為上*/  
* {  
-webkit-touch-callout:none;  
-webkit-user-select:none;  
-khtml-user-select:none;  
-moz-user-select:none;  
-ms-user-select:none;  
user-select:none;  
}  

 

js代碼-效果:防止復制+禁止右鍵兼容主流瀏覽器

很多時候我們自己網站上的原創文章不希望被別人直接復制拷貝走

下面我就給大家介紹一個JS,可以實現:防止復制+禁止右鍵的效果

並且兼容目前的各個瀏覽器。

話不多說,直接上代碼。

body {
-moz-user-select : none;
-webkit-user-select: none;
}
function iEsc(){ return false; }
function iRec(){ return true; }
function DisableKeys() {
if(event.ctrlKey || event.shiftKey || event.altKey)  {
window.event.returnValue=false;
iEsc();}
}
document.ondragstart=iEsc;
document.onkeydown=DisableKeys;
document.oncontextmenu=iEsc;
if (typeof document.onselectstart !="undefined")
document.onselectstart=iEsc;
else{//qsyz.net
document.onmousedown=iEsc;
document.onmouseup=iRec;
}
function DisableRightClick(www_qsyz_net){
if (window.Event){
if (www_qsyz_net.which == 2 || www_qsyz_net.which == 3)
iEsc();}
else
if (event.button == 2 || event.button == 3){
event.cancelBubble = true
event.returnValue = false;
iEsc();}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM