使用 Javascript 或 AngularJS 控制或禁用浏览器后退按钮
Control or Disable Browser Back Button with Javascript or AngularJS
使用 Javascript 或 AngularJS
控制或禁用浏览器后退按钮
我在这里不是在问问题,而是想展示一个解决方案,如果您使用 AngularJS 甚至 Javascript[=12=,您可以如何禁用和控制浏览器的后退按钮]
如果您只是使用 Javascript,您可以查看如何通过此 link:
禁用后退按钮
http://jordanhollinger.com/2012/06/08/disable-the-back-button-using-html5/
但是上面的代码不能很好地与 AngularJS 一起使用,因为 AngularJS 在后台使用 URL_Hash #,所以在这里我将展示如何扭转局面:
在你的主要 Javascript 代码中(不在 AngularJS 代码或控制器中),放入以下代码:
// *** 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
现在在您的 AngularJS 控制器中放置以下事件监听器:
/**
* 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();
});
希望对你有所帮助
此致
威尔
使用 Javascript 或 AngularJS
控制或禁用浏览器后退按钮我在这里不是在问问题,而是想展示一个解决方案,如果您使用 AngularJS 甚至 Javascript[=12=,您可以如何禁用和控制浏览器的后退按钮]
如果您只是使用 Javascript,您可以查看如何通过此 link:
禁用后退按钮http://jordanhollinger.com/2012/06/08/disable-the-back-button-using-html5/
但是上面的代码不能很好地与 AngularJS 一起使用,因为 AngularJS 在后台使用 URL_Hash #,所以在这里我将展示如何扭转局面:
在你的主要 Javascript 代码中(不在 AngularJS 代码或控制器中),放入以下代码:
// *** 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
现在在您的 AngularJS 控制器中放置以下事件监听器:
/**
* 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();
});
希望对你有所帮助
此致
威尔