Skip to content

Instantly share code, notes, and snippets.

@AitorAlejandro
Last active August 29, 2015 14:18
Show Gist options
  • Save AitorAlejandro/866c118f263d381ba1f9 to your computer and use it in GitHub Desktop.
Save AitorAlejandro/866c118f263d381ba1f9 to your computer and use it in GitHub Desktop.
Evita el pageback cuando pulsamos backspace en un formulario
/*
Este script evita que al pulsar sobre la tecla retroceso cuando estamos sobre un submit, o input text que sea
readonly el navegador haga un pageback.
Compatible desde IE7
Como añade un listener al documento está desarrollado con javascript puro por eficiencia.
*/
(function(d){
if(d.addEventListener) {
d.addEventListener('keydown',noPageBack,false);
} else {
d.attachEvent("onkeydown", noPageBack);
}
function noPageBack(e) {
var codigoTecla = (e.which) ? e.which : e.keyCode;
var elemento = (e.target) ? e.target : e.srcElement;
if( codigoTecla === 8 && (elemento.readOnly || elemento.type === 'submit') ) {
e.preventDefault ? e.preventDefault() : e.returnValue = false;
}
}
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment