I need to restrict backspace navigation in my whole app.Is there any solution i.e smething that i can do at one place for my whole app ?
Asked
Active
Viewed 808 times
0
-
Possible duplicate of [how to stop browser back button using javascript](http://stackoverflow.com/questions/12381563/how-to-stop-browser-back-button-using-javascript) – tex Jun 09 '16 at 08:37
-
@dev ? Found a solution for this? – Tirthraj Barot Jun 09 '16 at 11:11
3 Answers
1
angular.module('yourModule', [])
.controller('yourController', ['$scope', '$document', function($scope, $document) {
$document.on('keydown', function(e){
if(e.which === 8 && ( e.target.nodeName !== "INPUT" && e.target.nodeName !== "SELECT" ) ){ // you can add others here inside brackets.
e.preventDefault();
}
});
}
]);
It would prevent backspace navigation except for if any input tag is active.
We have not handled it for the textarea so it wouldn't allow backspace on textarea.
Here is the demo http://plnkr.co/edit/ZXtiJNI0a73c0SwIQ7mI?p=preview

Tirthraj Barot
- 2,671
- 2
- 17
- 33
1
This is the proper way:
preventNavigate.$inject = ['$window'];
function preventNavigate($window) {
return {
restrict: 'A',
link: function() {
$window.onbeforeunload = function() {
return "Are you sure you want to leave?";
}
}
}
}
app.directive('preventNavigate', preventNavigate);
Then on your index.html after your ng-app include the attribute prevent-navigate
;
<body ng-app="app" prevent-navigate>

Martin
- 15,820
- 4
- 47
- 56
0
You can create the keypress event directive like this and in the event callback you can e.preventDefault();
if key pressed is backspace.

Community
- 1
- 1

E. Abrakov
- 463
- 2
- 6