Skip to content

Instantly share code, notes, and snippets.

@webarthur
Last active October 13, 2016 13:52
Show Gist options
  • Save webarthur/a80700e8e9f9d6f1b3376176d7d5bdc9 to your computer and use it in GitHub Desktop.
Save webarthur/a80700e8e9f9d6f1b3376176d7d5bdc9 to your computer and use it in GitHub Desktop.
Desativar tecla backspace de voltar página do navegador
// detecta todas as teclas que estão sendo pressionadas
$(document).unbind('keydown').bind('keydown', function (event) {
// filtra apenas a tecla <backspace>
if (event.keyCode === 8) {
// variável que decidirá se a tecla será bloqueada
var doPrevent = true;
// tipos de campos permitidos
var types = ["text", "password", "file", "search", "email", "number", "date", "color", "datetime", "datetime-local", "month", "range", "search", "tel", "time", "url", "week"];
var d = $(event.srcElement || event.target);
var disabled = d.prop("readonly") || d.prop("disabled");
// checa se está nos campos permitidos e libera o <backspace> para edição
if (!disabled) {
if (d[0].isContentEditable) {
doPrevent = false;
} else if (d.is("input")) {
var type = d.attr("type");
if (type) {
type = type.toLowerCase();
}
if (types.indexOf(type) > -1) {
doPrevent = false;
}
} else if (d.is("textarea")) {
doPrevent = false;
}
}
// bloqueia a tecla
if (doPrevent) {
event.preventDefault();
return false;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment