Home >Web Front-end >JS Tutorial >jQuery disables keyboard back, blocks F5 refresh and disables right-click_jquery

jQuery disables keyboard back, blocks F5 refresh and disables right-click_jquery

WBOY
WBOYOriginal
2016-05-16 15:18:561346browse

The examples in this article introduce four ways to disable multiple functions in jquery

1. Disable F5 to refresh jQuery instance code
F5 has the function of refreshing the web page. Sometimes it may be necessary to disable this function. The following is a code example to introduce how to implement this function.
The code is as follows:

$(document).ready(function(){ 
 $(document).bind("keydown",function(e){ 
  var e=window.event||e; 
  if(e.keyCode==116){ 
   e.keyCode = 0; 
   return false; 
  } 
 }) 
})

2. jQuery disables keyboard back, F5 refresh and other shortcut keys

$(document).keydown(function(event){
   
     //屏蔽 Alt+ 方向键 ← 
     //屏蔽 Alt+ 方向键 →
     if ((event.altKey)&&((event.keyCode==37)||(event.keyCode==39)))  
     { 
        event.returnValue=false; 
        return false;
     }
 
     //屏蔽退格删除键 
     if(event.keyCode==8){
        return false; 
     }
        
     //屏蔽F5刷新键 
     if(event.keyCode==116){
        return false; 
     }
 
    //屏蔽alt+R 
    if((event.ctrlKey) && (event.keyCode==82)){
       return false; 
    }

 });

3. Disable the right-click function
The code is as follows:

$(document).ready(function() { 
$(document).bind("contextmenu",function(e) { 
alert("sorry! No right-clicking!"); 
return false; 
}); 
}); 

4. jQuery implementation code to prevent the backspace key from rewinding the web page

$(document).keydown(function (e) { 
var doPrevent; 
if (e.keyCode == 8) { 
var d = e.srcElement || e.target; 
if (d.tagName.toUpperCase() == 'INPUT' || d.tagName.toUpperCase() == 'TEXTAREA') { 
doPrevent = d.readOnly || d.disabled; 
} 
else 
doPrevent = true; 
} 
else 
doPrevent = false; 

if (doPrevent) 
e.preventDefault(); 
}); 

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn