How does JavaScript disable Backspace key_javascript?

Source: Internet
Author: User
This article mainly introduces how JavaScript disables the Backspace key, which can help you solve the problem of "rolling back to the previous page by pressing the Backspace key in the read-only input box in IE, if you are interested, you can refer to the findings in IE today. when you use the readonly = "readonly" attribute to set the text box to read-only There is a strange problem: if the cursor enters the read-only text box, and then press the Backspace key, it will jump to the previous page, the effect is like clicking the back button of the browser to return to the previous page, this problem does not occur in Firefox and google. to solve this problem, write the following solution: if the text box is read-only Disable the Backspace key.
The code is as follows:

// Handle keyboard events do not allow Backspace passwords or single-line, multi-line text boxes except 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 var vReadOnly = obj. getAttribute ('readonly'); // when processing null values, vReadOnly = (vReadOnly = "")? False: vReadOnly; // when you press the Backspace key, if the event source type is password or single-line or multi-line text, // And The readonly attribute is true or the enabled attribute is false, var flag1 = (ev. keyCode = 8 & (t = "password" | t = "text" | t = "textarea") & vReadOnly = "readonly ")? True: false; // when you press the Backspace key, if the event source type is non-password or single-line or multi-line text, the return key fails to be var flag2 = (ev. keyCode = 8 & t! = "Password" & t! = "Text" & t! = "Textarea ")? True: false; // judge if (flag2) {return false;} if (flag1) {return false;} window. onload = function () {// disable the backend key for Firefox and Opera document. onkeypress = banBackSpace; // disable the backend key to act on IE and Chrome document. onkeydown = banBackSpace ;}

With this processing, you can easily solve the problem of "rolling back to the previous page by pressing the Backspace key in IE in the read-only input box.

I hope you will like this article and continue to pay attention to the updated articles in the mini series.

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.