控制或禁用浏览器后退按钮JavaScript或AngularJS [英] Control or Disable Browser Back Button with Javascript or AngularJS

查看:3532
本文介绍了控制或禁用浏览器后退按钮JavaScript或AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Control or Disable Browser Back Button with Javascript or AngularJS

在这里,我不是问一个问题,但我想说明一个解决方案如何禁用和控制浏览器的后退按钮,如果你使用的是AngularJS甚至使用Javascript

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

推荐答案

如果你只是使用Javascript,您可以检查如何你可以从这个链接禁用后退按钮:

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

<一个href=\"http://jordanhollinger.com/2012/06/08/disable-the-back-button-using-html5/\">http://jordanhollinger.com/2012/06/08/disable-the-back-button-using-html5/

但是,这code以上将不能很好地AngularJS因为AngularJS使用URL_Hash#在后台工作,所以在这里,我会告诉你如何转身:

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:

在你的主要的Javascript code(里面没有AngularJS code或CONTROLER),把下面的code:

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

现在你里面AngularJS的Controler把下面的事件听者:

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.

此致

瓦埃勒

这篇关于控制或禁用浏览器后退按钮JavaScript或AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆