JavaScript禁用鼠标右键菜单
2011-09-15 00:00
561 查看
有些时候我们需要不让用户使用右键,比如不希望用户查看本页面源代码,或者复制本页的内容等等。当然用户可以用各种方法来达到目的,但是我们可以尽量增加他们达到这个目的的难度。使用以下js脚本就能够实现这个功能:
// function doProhibit()
{
if(window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu()
{
event.cancelBubble = true
event.returnvalue = false;
return false;
}
function norightclick(e)
{
if (window.Event)
{
if (e.which == 2 || e.which == 3)
return false;
}
else if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true
event.returnvalue = false;
return false;
}
}
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; //
}
// ]]>
但是有些时候,输入框的右键不能屏蔽,那么可以使用下面脚本:
<script type="text/javascript"> // <![CDATA[ function doProhibit() { if(window.Event) document.captureEvents(Event.MOUSEUP); function nocontextmenu() { event.cancelBubble = true event.returnvalue = false; return false; } function norightclick(e) { if (window.Event) { if (e.which == 2 || e.which == 3) return false; } else if (event.button == 2 || event.button == 3) { event.cancelBubble = true event.returnvalue = false; return false; } } document.oncontextmenu = nocontextmenu; // for IE5+ document.onmousedown = norightclick; // } // ]]> </script>
// function doProhibit()
{
if(window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu()
{
event.cancelBubble = true
event.returnvalue = false;
return false;
}
function norightclick(e)
{
if (window.Event)
{
if (e.which == 2 || e.which == 3)
return false;
}
else if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true
event.returnvalue = false;
return false;
}
}
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; //
}
// ]]>
但是有些时候,输入框的右键不能屏蔽,那么可以使用下面脚本:
if (document.layers) { document.captureEvents(Event.MOUSEDOWN); } document.onmousedown = click; document.oncontextmenu = new Function("return false;") function click(e) { e = e || event; if (e.button == 2) { var tag = e.srcElement || e.target; if (tag.type == "text" || tag.type == "textarea") { document.oncontextmenu = new Function("return true;") } else { document.oncontextmenu = new Function("return false;") } } }
相关文章推荐
- javascript 禁用鼠标右键菜单
- 破解网页鼠标右键菜单被禁用
- 深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制
- 用javascript实现既能禁止页面鼠标右键查看 同时又能查看页面文本框右键菜单
- JavaScript模拟鼠标右键菜单效果
- Javascript实现鼠标右键特色菜单
- 【ZedGraph】右键菜单和鼠标滚轴的移动缩放等功能的启用和禁用
- 用javascript实现禁用鼠标右键
- javascript鼠标右键菜单自定义效果
- 【ZedGraph】右键菜单和鼠标滚轴的移动缩放等功能的启用和禁用 (转)
- 原生javascript禁用和屏蔽鼠标右键
- 使用Javascript封装实现屏蔽鼠标右键系统键菜单,并绑定自定义的事件
- 深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制
- Javascript屏蔽鼠标右键菜单
- Javascript屏蔽鼠标右键菜单
- 用javascript实现既能禁止页面鼠标右键查看 同时又能查看页面文本框右键菜单
- 深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制
- Javascript实现鼠标右键特色菜单
- 网页禁用右键菜单和鼠标拖动选择方法小结
- javascript鼠标右键菜单自定义效果