JS註冊組|鎖右鍵、複製、選取文字


Posted by itiswonderfall on 2021-12-30

前言

前陣子因為工作需求,要幫客戶網站鎖右鍵,看到工程師貼上這段 code 所以上網查了一下,雖然還是有些 bug ,但現階段來說沒什麼大礙,看到網友 2013 年說「 真訝異現在還有人在玩鎖右鍵… 」,然後現在已經 2021 快邁入 2022 年我們一樣還在做這件事哈哈哈。

圖片來源

 


 

禁止選取文字

body {
    -webkit-touch-callout: none;  
    -webkit-user-select: none;  
    -khtml-user-select: none;  
    -moz-user-select: none;  
    -ms-user-select: none;  
    user-select: none;  
}

 

鎖右鍵、禁止使用 Ctrl 键、禁止使用 Shift 键、禁止使用 Alt 键

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
{
    document.onmousedown=iEsc;
    document.onmouseup=iRec;
}

function DisableRightClick(e)
{
    if (window.Event){ if (e.which == 2 || e.which == 3) iEsc();}
    else
        if (event.button == 2 || event.button == 3)
        {
            event.cancelBubble = true
            event.returnValue = false;
            iEsc();
        }
}

 


 

資料來源

  1. javascript 實現禁止右鍵,複製,選取文字 (相容firefox,IE,chrome等主流瀏覽器)...
  2. javascript 禁止右鍵相關疑問

#js #JS註冊組 #javascript #css #user-select #鎖右鍵







Related Posts

Inheritance

Inheritance

How to solve the perpetual loading issue in Evernote? Evernote 一直轉圈圈的解決辦法

How to solve the perpetual loading issue in Evernote? Evernote 一直轉圈圈的解決辦法

JS30 Day 25 筆記

JS30 Day 25 筆記


Comments