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