栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

如果您仅使用Javascript,则可以通过以下链接检查如何禁用“后退按钮”:

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 Codeif ( history_api ) history.pushState(null, '', '#StayHere');  // After the # you should write something, do not leave it empty

现在,在您的AngularJS Controler中放入以下事件列表器:

$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(); });

希望对您有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649526.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号