Last active
August 29, 2015 14:18
-
-
Save AitorAlejandro/866c118f263d381ba1f9 to your computer and use it in GitHub Desktop.
Evita el pageback cuando pulsamos backspace en un formulario
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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