火狐-火狐外包-北京火狐外包

搜 索

Search:火狐


JavaScriptjs禁止查看源文件屏蔽Ctrl+u/s、F12、右键等兼容IE火狐chrome

键盘事件键盘事件一般处理键盘按键事件我们采用这样的方式document.onkeydown=function (event) {//检测按下哪个键,作相应处理};键盘事件键盘事件一般处理键盘按键事件我们采用这样的方式document.onkeydown=function (event) {//检测按下哪个键,作相应处理};document.onkeydown=function (event) {//检测按下哪个键,作相应处理};event为键盘事件,对于chrome,firefox,IE(Edge),IE10,IE9均能支持function自带的e,而ie8以下只能识别windows.event,所以一般兼容写法为:event=event||window.event。获取按键码一般是event.keyCode,这个对各大浏览器都是兼容的。document.onkeydown=function (event) {event=event||window.event;var key=event.keyCode;//检测按下哪个键,作相应处理if(key==...){} };document.onkeydown=function (event) {event=event||window.event;var key=event.keyCode;//检测按下哪个键,作相应处理if(key==...){} };屏蔽浏览器默认事件的方法大致有三种:1)event.preventDefault()2)event.returnValue=false;3)return false;firefox的特殊方式支持是指,firefox这里一个比较坑爹的地方是,firefox需增加一个延迟才能生效,不然仍然会跳出浏览器的保存当前页面窗口,如下:document.onkeydown=function (event) {//判断按键 var key=event.keyCode;if(key== 83 && e.ctrlKey){/*延迟,兼容FF浏览器 */setTimeout(function(){alert('ctrl+s');},1);event.preventDefault();//或者是 return false;}document.onkeydown=function (event) {//判断按键 var key=event.keyCode;if(key== 83 && e.ctrlKey){/*延迟,兼容FF浏览器 */setTimeout(function(){alert('ctrl+s');},1);event.preventDefault();//或者是 return false;}而IE10,IE9,IE8以下对于event.returnValue=false的特殊方式支持是指键盘事件event必须为window.event时ctrl+s的默认事件才能屏蔽,在event=event||window.event的兼容写法中,IE8及以下的形参event是空,所以会取值为window.event,而IE10,IE9的function形参event是有效的,所以取值直接为event,因此IE10,IE9在写法为event=event||window.event时会屏蔽ctrl+s失效。假如要让所有IE版本能够屏蔽Ctrl+S,event取值只能是window.event了。由于window.event没有方法preventDefautl,所以屏蔽默认事件方法只能用return false;兼容IE、firefox、chrome,屏蔽Ctrl+s的写法为document.onkeydown=function (e) {e=window.event||e;if(key== 83 && e.ctrlKey){/*延迟,兼容FF浏览器 */setTimeout(function(){alert('www.qdxw.net');},1);return false;}};document.onkeydown=function (e) {e=window.event||e;if(key== 83 && e.ctrlKey){/*延迟,兼容FF浏览器 */setTimeout(function(){alert('www.qdxw.net');},1);return false;}};屏蔽鼠标右键的写法window.document.oncontextmenu = function (){alert('亲,你想干嘛,不要做坏事哦');return false; }window.document.oncontextmenu = function (){alert('亲,你想干嘛,不要做坏事哦');return false; }如果当前页面里含有iframe 那么iframe里的右键依然可用屏蔽页面里iframe里的右键写法document.getElementById('web').onload = function(){window.document.getElementById('web').contentWindow.document.oncontextmenu = function(){alert('www.qdxw.net');return false;} }document.getElementById('web').onload = function(){window.document.getElementById('web').contentWindow.document.oncontextmenu = function(){alert('www.qdxw.net');return false;} }web是iframe标签的ID属性。
标签: