JavaScript masking BACKSPACE keys

Source: Internet
Author: User

Found today in IE browser, when using the readonly= "ReadOnly" property to set the text box to read-only <input type= "text" readonly= "readonly"/> There is a strange problem: if the cursor into a read-only text box, and then press the BACKSPACE key, it will jump to the previous page, the effect is like clicking the browser Back button back to the previous page, and in Firefox and Google no such problem appears, in order to solve this problem, Write a procedure like this, if the text box is read-only, then disable the BACKSPACE key.

The code is as follows:

1 //handling keyboard events prevents back key (Backspace) passwords or single-line, multiline text boxes except2 function Banbackspace (e) {3         varEV = e | | Window.Event;//Get Event Object4         varobj = Ev.target | | Ev.srcelement;//Get Event Source5         vart = Obj.type | | Obj.getattribute ('type');//Get Event Source Type6         //gets the event type as a criterion7         varVreadonly = Obj.getattribute ('ReadOnly');8         //handling Null value cases9Vreadonly = (Vreadonly = ="") ?false: vreadonly;Ten         //when the BACKSPACE key is hit, the event source type is password or single line, multiline text, One         //and the ReadOnly property is True or the Enabled property is False, the BACKSPACE key fails A         varflag1= (Ev.keycode = =8&& (t=="Password"|| t=="text"|| t=="textarea")  -&& vreadonly=="ReadOnly")?true:false; -         //when the BACKSPACE key is hit, the event source type is non-password or single-line, multiline text, the BACKSPACE key is invalidated the         varflag2= (Ev.keycode = =8&& T! ="Password"&& T! ="text"&& T! ="textarea") -?true:false;  -          -         //Judging +         if(flag2) { -             return false; +         } A         if(FLAG1) { at             return false;  -         }    -     } -  -Window.onload=function () { -     //Disable Back button action on Firefox, Opera indocument.onkeypress=Banbackspace; -     //Disable back key action on IE, Chrome todocument.onkeydown=Banbackspace; +}

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

Transferred from: http://www.cnblogs.com/xdp-gacl/p/3785806.html

JavaScript masking BACKSPACE keys

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.