Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to block the backspace key by js

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces js how to block the backspace key, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

There is no absolute shielding, it is best to mix js with the server. The server determines whether the parameters of the page to be opened are opened for the first time, and if not, it jumps to the specified page. Here we only talk about the js method.

We often use JS to disable some keys on the keyboard in real project development, such as the backspace key (backspace/ backspace key). I have encountered disabling the backspace key when editing the page in a project, because the backspace key will cause the page to fall back, so the edited content will be lost, which is very mean. Ok, cut the crap and go straight to the code.

Better code, handle keyboard events prohibit back key (Backspace) password or single-line, multi-line text box except

/ / handle keyboard events prohibit back key (Backspace) password or single-line or multi-line 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 event type var vReadOnly = obj.getAttribute (' readonly') as a condition Var vEnabled = obj.getAttribute ('enabled'); / / handle null values vReadOnly = (vReadOnly = = null)? False: vReadOnly; vEnabled = (vEnabled = = null)? True: vEnabled; / / when tapping the Backspace key, the event source type is password or single-line or multi-line text, / / and the readonly attribute is true or enabled attribute is false, then the Backspace key expires var flag1= (ev.keyCode = 8 & & (password = "password" | | text = "text") & (vReadOnly==true | | vEnableddisabled true)? true:false) / / if the event source type is not a password or single-line or multi-line text when tapping the Backspace key, the backspace key fails var flag2= (ev.keyCode = = 8 & t! = "password" & & t! = "text" & & t! = "textarea")? true:false; / / judge if (flag2) {return false;} if (flag1) {return false }} / / prohibit back key action on Firefox, Opera _ document.onkeypress=banBackSpace; / / prohibit back key action on IE, Chrome _ document.onkeydown=banBackSpace

How to use it: put the above js code in between and ok it.

Here are some additions from other netizens

_ javascript:window.history.forward (1); / / disable fallback (for example, if you are currently on page B, click back to return to page A, then the code is written on page A, and then the code will not fall back to page An on page B) / / disable back key action on Firefox, Opera _ document.onkeypress=banBackSpace;// prohibit back key action on IE, Chrome _ document.onkeydown=banBackSpace / / handle keyboard events prohibit back key (Backspace) password or single-line or multi-line 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 event type var vReadOnly = obj.getAttribute (' readonly') as a condition Var vEnabled = obj.getAttribute ('enabled'); / / handle null values vReadOnly = (vReadOnly = = null)? False: vReadOnly; vEnabled = (vEnabled = = null)? True: vEnabled; / / when tapping the Backspace key, the event source type is password or single-line or multi-line text, / / and the readonly attribute is true or enabled attribute is false, then the Backspace key expires var flag1= (ev.keyCode = 8 & & (password = "password" | | text = "text") & (vReadOnly==true | | vEnableddisabled true)? true:false) / / when tapping the Backspace key, the event source type is not a password or single-line or multi-line text, then the backspace key expires var flag2= (ev.keyCode = = 8 & & t! = "password" & & t! = "text" & & t! = "textarea")? true:false; / / judge if (flag2) {return false;} if (flag1) {return false;}}

Shielding F5

/ / Shield F5 _ document.onkeydown = function (e) {e = window.event | | escape var keycode = e.keyCode | | e.whichscape if (e.ctrlKey | | e.altKey | | e.shiftKey | | keycode > = 112 & & keycode)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report