js禁止頁面複製功能禁用頁面右鍵菜單範例程式碼_javascript技巧

來源:互聯網
上載者:User
<body oncontextmenu="return false">禁用網頁右鍵菜單,但是仍然可以使用快速鍵複製。
js代碼禁用複製功能:
複製代碼 代碼如下:

<script type="text/javascript">
document.body.onselectstart=document.body.oncontextmenu=function(){ return false;}
</script>

注意這段代碼必須放在body元素後面,放在前面或者放在head裡面都不起作用。
補全:document.body.onselectstart 頁面選中功能。
document.body.oncontextmenu頁面右鍵菜單。
document.body.ondragstart頁面內容拖拽功能,拖拽是可以實現複製的。禁止複製時需要將其禁用。
document.body.oncopy頁面內容複寫功能,當禁用時,即使你點擊了複製或使用了快速鍵但是你剪下板中的內容不是你剛複製的內容而是你以前放在剪下板中的內容或為空白。
document.body.oncut頁面內容剪下功能,禁用和效果和禁用複製功能類似。
注意:當使用了上述禁用功能後,如果頁面的某個角落還可以右鍵或複製,那是因為你的body沒有覆蓋整個頁面,可以在body上添加如下屬性。
leftMargin=0 topMargin=0 style="width: 100%;height: 100%;"
通過設定body屬性來禁用複製功能代碼如下:
複製代碼 代碼如下:

<body oncontextmenu="return false" onselectstart="return false"
ondragstart="return false" oncopy="return false"
oncut="return false;
leftMargin=0
topMargin=0 style="width: 100%;height: 100%;" >
以下代碼是禁用網頁另存新檔但是我測試沒有成功,誰知道原因可以在下面給出評論,謝謝。
<noscript>
<iframe scr="*.htm"></iframe>
</noscript>
</body>

js代碼案例:
複製代碼 代碼如下:

//******************** 屏蔽右鍵 ***********************
function click(e) {
if (document.all) {
if (event.button==1||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=function(evt){
if(document.selection.createRange().parentElement().type == "file"){
return false;
}
if ((event.keyCode==116)|| //屏蔽 F5 重新整理鍵
(event.ctrlKey && event.keyCode==82)){ //Ctrl + R
event.keyCode=0;
event.returnValue=false;
}
if ((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4
return false;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.