JS禁用右键、禁用Ctrl+u、禁用Ctrl+s、禁用F12的实现代码
先来两个简单粗暴的
第一个常用的函数代码
functionkeycodes(){ //禁止右键 document.oncontextmenu=function(){returnfalse}; document.onkeydown=function(e){ e=window.event||e; vark=e.keyCode; //屏蔽ctrl+u,F12键 if((e.ctrlKey==true&&k==85)||k==123){ e.keyCode=0; e.returnValue=false; e.cancelBubble=true; returnfalse; } } }
第二种方法:
vararr=[123,17,18]; document.oncontextmenu=newFunction("event.returnValue=false;"),//禁用右键 document.onselectstart=newFunction("event.returnValue=false;"),//禁用选中 window.onkeydown=function(e){ varcode=e.keyCode; if(arr.indexOf(code)>-1){ console.log("中招了",code); e.preventDefault(); } },window.oncontextmenu=function(e){ e.preventDefault(); }
第三种方法:
vararr=[123,17,18]; document.oncontextmenu=newFunction("event.returnValue=false;"),//禁用右键 window.onkeydown=function(e){ varkeyCode=e.keyCode||e.which||e.charCode; varctrlKey=e.ctrlKey||e.metaKey; console.log(keyCode+"--"+keyCode); if(ctrlKey&&keyCode==85){ console.log("ctrl+u"); e.preventDefault(); } if(arr.indexOf(keyCode)>-1){ console.log("其他"); e.preventDefault(); } //e.preventDefault(); //returnfalse; }
一些常用的代码
1、实现禁止选中文字。
2、实现禁止鼠标右键。
3、实现禁止F12审查元素。
4、实现禁止Ctrl+U查看源代码。
5、实现禁止Ctrl+S网页另存为。
6、实现禁止F5刷新页面。
all、如果想同时实现以上的几种效果,如下:
JavaScript屏蔽右键F12和Ctrl+U等绝大部分按键
//屏蔽右键菜单 document.oncontextmenu=function(event){ if(window.event){ event=window.event; } try{ varthe=event.srcElement; if(!((the.tagName=="INPUT"&&the.type.toLowerCase()=="text")||the.tagName=="TEXTAREA")){ returnfalse; } returntrue; }catch(e){ returnfalse; } } //屏蔽粘贴 document.onpaste=function(event){ if(window.event){ event=window.event; } try{ varthe=event.srcElement; if(!((the.tagName=="INPUT"&&the.type.toLowerCase()=="text")||the.tagName=="TEXTAREA")){ returnfalse; } returntrue; }catch(e){ returnfalse; } } //屏蔽复制 //屏蔽剪切 document.oncut=function(event){ if(window.event){ event=window.event; } try{ varthe=event.srcElement; if(!((the.tagName=="INPUT"&&the.type.toLowerCase()=="text")||the.tagName=="TEXTAREA")){ returnfalse; } returntrue; }catch(e){ returnfalse; } } //禁止f12 functionfuckyou(){ window.close();//关闭当前窗口(防抽) window.location="about:blank";//将当前窗口跳转置空白页 } //禁止Ctrl+U vararr=[123,17,18]; document.oncontextmenu=newFunction("event.returnValue=false;"),//禁用右键 window.onkeydown=function(e){ varkeyCode=e.keyCode||e.which||e.charCode; varctrlKey=e.ctrlKey||e.metaKey; console.log(keyCode+"--"+keyCode); if(ctrlKey&&keyCode==85){ e.preventDefault(); } if(arr.indexOf(keyCode)>-1){ e.preventDefault(); } } functionck(){ console.profile(); console.profileEnd(); //我们判断一下profiles里面有没有东西,如果有,肯定有人按F12了,没错!! if(console.clear){ console.clear() }; if(typeofconsole.profiles=="object"){ returnconsole.profiles.length>0; } } functionhehe(){ if((window.console&&(console.firebug||console.table&&/firebug/i.test(console.table())))||( typeofopera== 'object'&&typeofopera.postError=='function'&&console.profile.length>0)){ fuckyou(); } if(typeofconsole.profiles=="object"&&console.profiles.length>0){ fuckyou(); } } hehe(); window.onresize=function(){ if((window.outerHeight-window.innerHeight)>200) //判断当前窗口内页高度和窗口高度,如果差值大于200,那么呵呵 fuckyou(); } document.onkeydown=function(event){ if((event.keyCode==112)||//屏蔽F1 (event.keyCode==113)||//屏蔽F2 (event.keyCode==114)||//屏蔽F3 (event.keyCode==115)||//屏蔽F4 //(event.keyCode==116)||//屏蔽F5 (event.keyCode==117)||//屏蔽F6 (event.keyCode==118)||//屏蔽F7 (event.keyCode==119)||//屏蔽F8 (event.keyCode==120)||//屏蔽F9 (event.keyCode==121)||//屏蔽F10 (event.keyCode==122)||//屏蔽F11 (event.keyCode==123))//屏蔽F12 { returnfalse; } } window.onhelp=function(){ returnfalse; }
网页禁止右键相关行为代码:禁止复制、禁止F12、禁止ctrl+u、网站禁止右键
防止网站文章被转载的一些常规方法,禁止复制、禁止F12、禁止ctrl+u、网站禁止右键;但是这些只是做了一层防护,应对普通的新手编辑足够了,如果别人真想复制你网站的内容,还是有其他方法可以进行复制粘贴的。
下面是代码:
$("body").bind('copy',function(e){ varbody_element=document.getElementsByTagName('body')[0]; varselection=window.getSelection(); varnewdiv=document.createElement('div');newdiv.style.position='absolute'; newdiv.style.left='-99999px'; body_element.appendChild(newdiv); newdiv.appendChild(selection.getRangeAt(0).cloneContents()); newdiv.innerHTML="\u539f\u6587\uff1a"+document.location.href+"\u53d7\u539f\u521b\u534f\u8bae\u4fdd\u62a4\uff0c\u6d4f\u89c8\u5668\u6682\u4e0d\u652f\u6301\u590d\u5236\u7c98\u8d34"; selection.selectAllChildren(newdiv);window.setTimeout(function(){body_element.removeChild(newdiv)},200) });
上述代码是当选中页面文字,按ctrl+c键进行复制时,会弹出提示框阻止复制操作。
其中,
document.location.href为当前页面url;
\u539f\u6587\uff1a进行了Unicode编码了的中文字符,转码过来就是“原文:”;
\u53d7\u539f\u521b\u534f\u8bae\u4fdd\u62a4\uff0c\u6d4f\u89c8\u5668\u6682\u4e0d\u652f\u6301\u590d\u5236\u7c98\u8d34同样的是Unicode编码后的字符串,中文意思为:受原创协议保护,浏览器暂不支持复制粘贴;
Unicode编码内容可以借助一些工具进行修改,这里给出站长工具Unicode编码转换链接:http://tool.chinaz.com/tools/unicode.aspx
//按键触发 document.onkeydown=function(){ //禁止ctrl+u if(event.ctrlKey&&window.event.keyCode==85){ returnfalse; } //禁止F12 if(window.event&&window.event.keyCode==123){ event.keyCode=0; event.returnValue=false; } } //网站禁止右键 document.body.oncontextmenu=function(){ self.event.returnValue=false };
上面代码是当按下相关按键或者按下鼠标右键触发的提示。
网页如何禁止鼠标右键和F12,键盘另存为、粘贴、复制事件
最近做的商用后台项目涉及到前端加密技术,为了保证前端页面展示的数据不被窃取,所以在js上做了一些处理,这些处理并不能完全杜绝有心之人窃取数据,只是加大他们操作的难度而已。
1.禁止浏览器右键事件。使用浏览器右键可以将网页保存下来,或者审查网页源代码,这是我们想避免的(当然,仍然可以通过浏览器右上角的工具栏打开开发者工具,这里可以判断下鼠标的光标位置,如果光标移动到导航栏及页面body之外的地方,我们可以选择把页面展示的元素清空,这样在控制台看见的源代码也没有展示出来的数据了)。
js实现:
禁止鼠标右键事件 这里是一些内容,展示禁止鼠标右键事件
jquery实现:
禁止鼠标右键事件 这里是一些内容,展示禁止鼠标右键事件
2.禁止F12事件,键盘的选择粘贴复制事件
jquery实现:
禁止键盘粘贴复制 这里是一些内容
js实现:
js禁止f12事件 这里是一些内容
js屏蔽chromeF12后页面自动跳转
functiona(){ window.location.href="https://www.nhooo.com/"; returnfalse } if(top.window.outerHeight-top.window.innerHeight>150){ a() } if(top.window.outerWidth-top.window.innerWidth>150){ a() } }; r(document).keydown(function(){ returny(arguments[0]) }); functiony(r){ varo; if(window.event){ o=r.keyCode }elseif(r.which){ o=r.which } if(o==123){ e.larryCmsError("说好的demo版本只演示不允许F12哦!",e.larryCore.tit); returnfalse } if(r.ctrlKey){ if(r.shiftKey&&o==73){ e.larryCmsError("说好的demo版本只演示不允许ctrl+shift+I哦!",e.larryCore.tit); returnfalse } } if(r.ctrlKey&&o==83){ e.larryCmsError("说好的demo版本只演示不允许ctrl+s保存哦!",e.larryCore.tit); returnfalse } }
具体就是页面加载后执行s();判断是不是有调试工具
和按f12后触发window.onresize判断,和按键key判断
到此这篇关于JS禁用右键、禁用Ctrl+u、禁用Ctrl+s、禁用F12的实现代码的文章就介绍到这了,更多相关JS禁用右键Ctrl内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。