Home  >  Article  >  Web Front-end  >  JS disables browser backspace key implementation ideas and code_javascript skills

JS disables browser backspace key implementation ideas and code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:18:20897browse
Copy code The code is as follows:

function forbidBackSpace(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 the event source type
//Get the event type as the judgment condition
var vReadOnly = obj.readOnly;
var vDisabled = obj.disabled;
//Handle the undefined value situation
vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;
vDisabled = (vDisabled == undefined) ? true : vDisabled;
//When the Backspace key is pressed, the event source type is password or single or multi-line text ,
//If the readOnly attribute is true or the disabled attribute is true, the backspace key is invalid
var flag1 = ev.keyCode == 8 && (t == "password" || t == " text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
//When the Backspace key is pressed, the event source type is not password or single or multi-line text, then The backspace key is disabled
var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";
//Judge
if (flag2 || flag1) return false;
}
//Forbid the back key to work on Firefox and Opera
// document.onkeypress = forbidBackSpace;
//Forbid the back key to work on IE and Chrome
document.onkeydown = forbidBackSpace;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn