1

I'm trying to prevent backspace from navigating back to the previous page with my code

$(document).keydown(function (e) {

    if (e.keyCode === 8) {

        alert("Backspace is pressed");

        e.preventDefault;
        return false;

    } else {

    }

});

It's working except when I focus on a select tag then press backspace it ignores the function I created.

Tushar
  • 85,780
  • 21
  • 159
  • 179
sinlee
  • 45
  • 5

1 Answers1

0

See here

The code:

window.addEventListener('keydown', function(e) {

  var key = e.keyCode || e.which;
  if (key == 8 /*BACKSPACE*/ || key == 46/*DELETE*/) {
    var len=window.location.href.length;
    if(window.location.href[len-1]!='#') window.location.href += "#";
  }
},false);

JSFIDDLER

Community
  • 1
  • 1
AlexBerd
  • 1,368
  • 2
  • 18
  • 39