Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  31] [ 2]  / answers: 1 / hits: 33248  / 12 Years ago, wed, january 30, 2013, 12:00:00

In my chat application there are some text fields which gets the user login details.


When filling the user details, if a user suddenly pressed the ESC key, the data will be lost.


If need to disable the function of ESC key, which event I need to use? How can I do that.


My JavaScript code is:


function esc(e){
e = e || window.event || {};
var charCode = e.charCode || e.keyCode || e.which;
if(charCode == 27){
return false;
}
}

I searched a lot in Stack Overflow and Google; nothing worked. Please, can anyone help me to do that?


More From » dom-events

 Answers
57

I got the solution to control the F5 , Esc , BackSpace(BS) keys with the following code.



My Java Script code will be ,



document.attachEvent(onkeydown, win_onkeydown_handler);

function win_onkeydown_handler() {
switch (event.keyCode) {

case 116 : // 'F5'
event.returnValue = false;
event.keyCode = 0;
break;

case 27: // 'Esc'
event.returnValue = false;
event.keyCode = 0;
break;

case 08: // 'BackSpace'
if (event.srcElement.tagName == INPUT
|| event.srcElement.tagName == TEXTAREA) {
} else {
event.returnValue = false;
event.keyCode = 0;
}
break;

}
}


Thanks who are all supported me to do this and for your suggestions.


[#80528] Tuesday, January 29, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;