How JavaScript disables backspace keys _javascript tips

Source: Internet
Author: User

Today found in IE browser, when the use of the readonly= "ReadOnly" property to set the text box to read-only <input type= "text" readonly= "readonly"/> A strange problem with is that if the cursor goes into a read-only text box and then presses the BACKSPACE key, it jumps to the previous page, as if clicking the Back button on the browser returns to the previous page, and there is no such problem with Firefox and Google. , to solve this problem, write a method such as the following, if the text box is read-only, then disables the backspace key . The
code is as follows:

/handling keyboard events prohibit back key (Backspace) password or single line, multiline text box exception function Banbackspace (e) {var ev = e | | window.event;// Get Event object var obj = Ev.target | | ev.srcelement;//get event source var t = Obj.type | |
  Obj.getattribute (' type '); Get event Source Type//Get the event type as a condition of judgment var vreadonly = Obj.getattribute (' readonly '); Handle NULL value condition Vreadonly = (Vreadonly = = "")?
  False:vreadonly; 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 Enabled property is False, and the BACKSPACE key fails var flag1= (Ev.keycode = = 8 &&
  (t== "Password" | | t== "TEXT" | | t== "TEXTAREA") && vreadonly== "ReadOnly")? The BACKSPACE key fails var flag2= (Ev.keycode = 8 && t!= "password" && t!= "text" backspace when the event source type is not a password or a single line, multi-line text, when the key is tapped &A  
  
  mp;& T!= "textarea")? True:false;
  Judge if (Flag2) {return false; 
  } if (Flag1) {return false;
 } window.onload=function () {//No Back key action on Firefox, Opera Document.onkeypress=banbackspace;
Prohibit the back key role in IE, Chrome document.onkeydown=banbackspace; }

With such processing, you can easily solve the "read-only input box under IE press the BACKSPACE key back to the previous page" problem.

I hope you like this article, continue to focus on the small series of updates and finishing articles.

Related Article

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.