Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  132] [ 6]  / answers: 1 / hits: 31065  / 9 Years ago, mon, march 30, 2015, 12:00:00

Control or Disable Browser Back Button with Javascript or AngularJS



Here i am not asking a question but i want to show a solution to how you can disable and control Browser's Back Button if you are using AngularJS or even with Javascript


More From » angularjs

 Answers
1

If you are just using Javascript you can check how you can disable Back Button from this link:



http://jordanhollinger.com/2012/06/08/disable-the-back-button-using-html5/



But this code above will not work well with AngularJS because AngularJS uses URL_Hash # in the background, so here i will show how you can turn around:



In your main Javascript Code (Not inside AngularJS Code or controler), put the following code:



// *** Author: Wael Sidawi
// **** Deactive Back Button ****
var history_api = typeof history.pushState !== 'undefined';
// history.pushState must be called out side of AngularJS Code
if ( history_api ) history.pushState(null, '', '#StayHere'); // After the # you should write something, do not leave it empty


And now inside your AngularJS Controler put the following event listner:



/**
* Event-Listner for Back-Button
*/
$scope.$on('$locationChangeStart', function(event, next, current){
// Here you can take the control and call your own functions:
alert('Sorry ! Back Button is disabled');
// Prevent the browser default action (Going back):
event.preventDefault();
});


I hope it could help you.



Best Regards



Wael


[#67255] Friday, March 27, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
micheleb

Total Points: 275
Total Questions: 103
Total Answers: 103

Location: Macau
Member since Sat, Jul 11, 2020
4 Years ago
micheleb questions
;