JS prevents backspace from returning to the previous interface (compatible with various browsers)

Source: Internet
Author: User

//Handle keyboard event disable back key (Backspace) password or single line, multiline text box except
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
//Gets the type of event that is used as the judging condition
var vreadonly = Obj.getattribute (' readonly ');
var venabled = Obj.getattribute (' enabled ');
//Handling Null values case
vreadonly = (vreadonly = = null)? false:vreadonly;
venabled = (venabled = = null)? true:venabled;
//When the BACKSPACE key is hit, the event source type is password or single line, multiline text,
///And the ReadOnly property is True or the Enabled property is False, the BACKSPACE key fails
var Flag1 = (Ev.keycode = = = 8 && (t = = "password" | | t = "text" | | t = "TEXTAREA")
&& (vreadonly = = True | | venabled! = TRUE))? True:false;
//When the BACKSPACE key is hit, the event source type is non-password or single-line, multiline text, the BACKSPACE key fails
var Flag2 = (Ev.keycode = = 8 && t! = "password" && t! = "text" && t! = "textarea")? True: false;
//Judgment
if (flag2) {
return false;
}
if (Flag1) {
return false;
}
}

//Disable Back button action on Firefox, Opera
document.onkeypress = banbackspace;
//Disable back key action on IE, Chrome
document.onkeydown = banbackspace;

JS prevents backspace from returning to the previous interface (compatible with various browsers)

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.