JavaScript blocks browser Back events prevent misoperation clear form _javascript tips

Source: Internet
Author: User
Because most customers are not computer-literate big rookie. So the wrong operation is more, sometimes do not select the text box, and then accidentally pressed the backspace, just hard to fill out the form on the back of nothing. Search the Internet a lot of information, and then integrated a bit. Share to everyone. I also hope that the yogurt sister to see my first technical blog. Oh, don't mind me mentioning you. )。 Don't say much nonsense. On the source.
Copy Code code as follows:

$ (function () {
Handle keyboard events No 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

Get the event type as a criterion
var vreadonly = Obj.getattribute (' readonly ');
var venabled = Obj.getattribute (' enabled ');
Handling Null value conditions
Vreadonly = (vreadonly = null)? False:true;
venabled = (venabled = null)? true:venabled;

When you hit the BACKSPACE key, the event source type is a password or a single line, multi-line text,
And the ReadOnly property is True or the Enabled property is False, the BACKSPACE key is invalidated
var flag1= (Ev.keycode = = 8 && (t== "Password" | | t== "TEXT" | | t== "TEXTAREA")
&& (vreadonly | | venabled!=true)) True:false;

When the BACKSPACE key is hit, the event source type is not a password or a single line or multi-line text, the BACKSPACE key is invalidated
var flag2= (Ev.keycode = = 8 && t!= "password" && t!= "text" && t!= "textarea")
? true:false;

Judge
if (Flag2) {
return false;
}
if (FLAG1) {
return false;
}
}

No back key for Firefox, Opera
Document.onkeypress=banbackspace;
No back key acts on IE, Chrome
Document.onkeydown=banbackspace;
Window.history.forward (1)//Screen browser with the back key
})

the areas to be noted are:

This piece of code must be placed in the initialization method. Then use Document.onkeypress=banbackspace to call your own method of writing.
Copy Code code as follows:

Vreadonly = (vreadonly = null)? False:true for this code, the original colon is followed by the Vreadonly<span style= "font-family:arial, Helvetica, Sans-serif;" The value of > was later found to be null in my project, not null and changed to TRUE. </span>

The above method enables you to block the browser back event by pressing the backspace when Text,password,textare, but does not block the backspace event when the text box has a value. This is my first blog, I hope you have a lot to join in.
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.