Javascript prevents browser back events from misoperation _ javascript tips

Source: Internet
Author: User
Due to many misoperations, sometimes the text box is not selected, And then accidentally press the backspace, the forms that have just been filled out will also be removed. To address this problem, this article provides a detailed solution, so do not miss it because most customers do not understand computers. Therefore, there are many misoperations. Sometimes the text box is not selected, and after you accidentally press the backspace, the forms that have just been filled out will be removed. I checked a lot of information online and integrated it. Share with you. I also hope that my yogurt sister will visit my first technical blog. Haha (don't mind if I mention you .). Not much nonsense. Source code.

The Code is as follows:


$ (Function (){
// Handle Keyboard Events. Do not use Backspace passwords or single-line or multi-line text boxes
Function banBackSpace (e ){
Var ev = e | window. event; // obtain the event object
Var obj = ev.tar get | ev. srcElement; // obtain the event Source

Var t = obj. type | obj. getAttribute ('type'); // obtain the event source type.

// Obtain the event type used as the judgment Condition
Var vReadOnly = obj. getAttribute ('readonly ');
Var vEnabled = obj. getAttribute ('enabled ');
// Process null values
VReadOnly = (vReadOnly = null )? False: true;
VEnabled = (vEnabled = null )? True: vEnabled;

// When you press the Backspace key, the event source type is password or single-line or multi-line text,
// If the readonly attribute is true or the enabled attribute is false, the Return key is invalid.
Var flag1 = (ev. keyCode = 8 & (t = "password" | t = "text" | t = "textarea ")
& (VReadOnly | vEnabled! = True ))? True: false;

// When you press the Backspace key, the Backspace key is invalid if the event source type is non-Password, single-line, or multi-line text.
Var flag2 = (ev. keyCode = 8 & t! = "Password" & t! = "Text" & t! = "Textarea ")
? True: false;

// Judge
If (flag2 ){
Return false;
}
If (flag1 ){
Return false;
}
}

// Disable the backend key for Firefox and Opera
Document. onkeypress = banBackSpace;
// Disable the backend key for IE and Chrome
Document. onkeydown = banBackSpace;
Window. history. forward (1); // block the back key of the browser.
})


Note the following:

This code must be put in the initialization method. Then, use document. onkeypress = banBackSpace to call the method you have written.

The Code is as follows:


VReadOnly = (vReadOnly = null )? False: true; for this piece of code, the original colon is followed by the value of vReadOnly, and then it is found in my project that it returns null. If it is not null, it is changed to true.


You can follow the above method to block the browser's backend events in non-text, password, and textare, but it does not block the backend events when there is a value in the text box. This is my first blog, and I hope you can join me more.
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.