Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript或AngularJS控制或禁用浏览器后退按钮_Javascript_Angularjs_Button_Browser_Back - Fatal编程技术网

使用Javascript或AngularJS控制或禁用浏览器后退按钮

使用Javascript或AngularJS控制或禁用浏览器后退按钮,javascript,angularjs,button,browser,back,Javascript,Angularjs,Button,Browser,Back,使用Javascript或AngularJS控制或禁用浏览器后退按钮 这里我不是在问问题,但我想展示一个解决方案,说明如果您使用AngularJS,甚至使用Javascript,如何禁用和控制浏览器的后退按钮如果您仅使用Javascript,您可以检查如何从该链接禁用后退按钮: 但是上面的代码不能很好地与AngularJS配合使用,因为AngularJS在后台使用URL_Hash#,所以这里我将展示如何扭转局面: 在主Javascript代码(不在AngularJS代码或Controller中

使用Javascript或AngularJS控制或禁用浏览器后退按钮


这里我不是在问问题,但我想展示一个解决方案,说明如果您使用AngularJS,甚至使用Javascript,如何禁用和控制浏览器的后退按钮如果您仅使用Javascript,您可以检查如何从该链接禁用后退按钮:

但是上面的代码不能很好地与AngularJS配合使用,因为AngularJS在后台使用URL_Hash#,所以这里我将展示如何扭转局面:

在主Javascript代码(不在AngularJS代码或Controller中)中,输入以下代码:

// *** 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();            
});
我希望它能帮助你

致意


Wael

对于angularjs部分,只需使用这篇文章,有一大堆类似的问题,所以不要立即发布…@FerTo为什么不使用$location.replace()?@Wael,上面的代码用于禁用后退按钮。但它甚至在登录时禁用了ng hrefpage@Hema不幸的是,它禁用了角…@JosefE中的所有导航。是的,我注意到了。我用另一种方式克服了这个问题。