js 屏蔽右键菜单兼容FireFox

本文介绍了一种禁用网页上的鼠标右键菜单、拖拽、选中文本以及F5刷新功能的方法。通过在HTML <body> 标签内加入特定的JavaScript代码,可以有效防止用户进行上述操作。这对于保护网站内容不被轻易复制有一定作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1种方式:
实现原理是,在<body>语法标签里加入如下代码:
ondragstart="window.event.returnValue=false;" oncontextmenu="window.event.returnValue=false;" onselectstart="event.returnValue=false;"

这里,鼠标拖曳(ondragstart)、选择(onselectstart)和右键弹出pop菜单。

2种方式:
在引用js文件中加入
document.oncontextmenu=function(){
return false;
};
document.ondragstart=function(){
return false;
};
document.onselectstart=function(){
return false;};
document.onbeforecopy=function(){
return false;};
document.onselect=function(){
document.selection.empty();
};
document.oncopy=function(){
document.selection.empty();
};
document.onmouseup=function(){
document.selection.empty();
};

3.屏蔽F5键刷新
<script>
function DisableF5(){
with (event){
// F5 and Ctrl+R
if (keyCode==116 || (ctrlKey && keyCode==82)){
event.keyCode = 0;
event.cancelBubble = true;
return false;
}
}
}
document.onkeydown = DisableF5;
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值