JS Disable browser backspace key implementation ideas and code _javascript skills

Source: Internet
Author: User
Tags call back

A project was submitted last week (used internally), together with a bug, said to disable the BACKSPACE key (BACKSPACE or call Back button), because this and his habits are not the same, fearing that if the text input half, he pressed the backspace key, the whole page of the content of white filled, Then vowed to say that he did the system back key is not used, I have a problem with this system, because of many things, only to record the problem, and then check the next he did before the project, and did not handle the backspace. My project has not handled, to me this shout come, forget, not and young stoop. Do not disable a back button, simple.

In fact, said disable is not completely disabled, the back key in the browser defaults to click the Back button, as long as the normal text input can also be used, in other cases the BACKSPACE key is banned. Look at the code.

When the keyboard knocks down the back key (Backspace)
1, prevent the browser automatically back
2, but does not affect the password, single line text, multiline text input box, such as the fallback operation

Solution:

Search on the Internet, found that there are many solutions, compared to the Zywang scheme better

On its basis, to complement and perfect to meet the needs, after finishing the code as follows:
Code one, core code:

 function Forbidbackspace (e) {var ev = e | | window.event;//Get Event object var obj = Ev.targe T | | Ev.srcelement; Get event source var t = Obj.type | | Obj.getattribute (' type '); 
Gets the event source type//Gets the event type as a condition of judgment var vreadonly = obj.readonly; 
var vdisabled = obj.disabled; Handling undefined value vreadonly = (vreadonly = = undefined)? 
False:vreadonly; vdisabled = (vdisabled = = undefined)? 
true:vdisabled; When the BACKSPACE key is hit, the event source type is a password or single line, multiline text,//And the ReadOnly property is true or the disabled property is true, the BACKSPACE key expires var Flag1 = Ev.keycode = 8 && ( t = = "Password" | | t = = "Text" | | 
t = = "textarea") && (vreadonly = = True | | | vdisabled = = TRUE); The BACKSPACE key fails var flag2 = Ev.keycode = 8 && t!= "password" && t!= "text" When the event source type is not a password or a single line or multi-line text when knocking the BACKSPACE key &A 
mp;& T!= "textarea"; 
Judge if (Flag2 | | flag1) return FALSE; 
}//No back key acts on Firefox, Opera//document.onkeypress = Forbidbackspace; No back key acts on IE, Chrome document.onkeydown = forbidbackspace; 

Code two,

 function Bindbackevent () {//Prevent BACKSPACE keys
 	$ (document). KeyDown (function (e) {
 		e = window.event | | e;
	 	var code = E.keycode | | E.which;
	 	if (code = = 8) {
	 		var src = e.srcelement | | e.target;
	 		var tag = src.tagname;
	 		if (tag!= "INPUT" && tag!= "TEXTAREA") {
	 			e.returnvalue = false; 
	 			return false;
	 		} else if ((tag = = "INPUT" | | | tag = = "TEXTAREA") && src.readonly = = True) {
	 			e.returnvalue = false;
	 			return false;}}}
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.