<script>
document.oncontextmenu = function() { return false; };
</script>
或綁定 mousedown 攔截右鍵:
document.onmousedown = function(e) {
e = e || window.event;
if (e.button === 2) { alert("禁止右鍵"); return false; }
};
<body oncontextmenu="return false" onselectstart="return false"
ondragstart="return false" onbeforecopy="return false"
oncopy="document.selection.empty()" onselect="document.selection.empty()">
<script>
function disableselect(e) { return false; }
function reEnable() { return true; }
// IE
document.onselectstart = new Function("return false");
// Firefox/舊版
if (window.sidebar) {
document.onmousedown = disableselect;
document.onclick = reEnable;
}
</script>
<style>
body {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>
此法不干擾 JS 事件,純樣式層面禁止使用者選取文字,相容性佳。
<noscript><iframe src=""></iframe></noscript>
利用 <noscript> 在禁用 JS 環境下載入空 iframe 干擾另存新檔(效果有限,屬防君子不防小人)。
<script>
document.onkeydown = function(e) {
e = e || window.event;
// 禁用 F5 / Ctrl+F5 重新整理
if (e.keyCode === 116 || (e.ctrlKey && e.keyCode === 116)) {
e.keyCode = 0; e.returnValue = false; return false;
}
// 禁用 Ctrl+C 複製
if (e.ctrlKey && e.keyCode === 67) { return false; }
// 禁用 Alt+F4 關閉視窗(僅 IE 旧版有效)
if (e.altKey && e.keyCode === 115) { window.close(); }
};
</script>
function SetCopyContent() {
event.returnValue = false;
var content = document.title + "\r\n";
content += document.getElementById("txt1").value + "\r\n";
content += "本資源來自 " + location.href;
window.clipboardData.setData('Text', content);
alert("複製成功,請推薦給好友!");
}
<body oncopy="SetCopyContent()">