简体   繁体   English

如何禁用AngularJS中的浏览器后退按钮?

[英]How to disable browser back button in AngularJS?

在此处输入图片说明

在此处输入图片说明

i want disable browser back button and refresh button. 我想禁用浏览器后退按钮和刷新按钮。 in angular 在角度

example my source 我的例子

page source : 页面来源:

"use strict";

.controller("wizardSecondController", function($scope, $state, $stateParams, wizardManager) {

$scope.$on("$viewContentLoaded", function(event, data) {
});

i want prevent browser back and refresh . 我想防止浏览器返回并刷新。 please answer . 请回答 。

If you want to disable browser back button in angular, please try out the following code. 如果您想禁用浏览器的后退按钮,请尝试以下代码。 It may help. 这可能会有所帮助。

$scope.$on('$locationChangeStart', function(event, next, current){
    event.preventDefault();            
});

It's not a very straight forward thing in Angular.js but you can use JavaScript function - window.onhashchange to disable to back button. 这不是在Angular.js一个非常简单的事情,但你可以使用JavaScript函数- window.onhashchange以禁用后退按钮。

Take a look at this link , you might get better ideas. 看一下此链接 ,您可能会得到更好的主意。

Use javascript event 使用JavaScript事件

window.onbeforeunload = function(e) {
      var dialogText = 'Dialog text here';
      e.returnValue = dialogText;
      return dialogText;
    };

refer 参考

I shall give you advice as to disabling the back button: 我将为您提供有关禁用后退按钮的建议:

Strictly speaking it isn't "possible" if using standards, but there are workarounds. 严格来说,如果使用标准,这是“不可能的”,但是有解决方法。

There is the window.history object which includes a small API . 有一个window.history对象,其中包含一个小的API

But it doesn't allow you to double check for states/pages before the user surfed to your site. 但是,它不允许您在用户浏览您的网站之前仔细检查状态/页面。 Obviously for security reasons and not by accident or missing implementation. 显然是出于安全原因,并非偶然或未实施。

There's various checks for the usage of navigating back in the history and several posts about that topic, but none is helpful as to when it comes to the point the user leaves your page and goes beyond your accessible history. 有很多检查历史回溯的用法,以及有关该主题的几篇文章,但是对于何时用户离开页面并超出可访问的历史记录,这些方法都无济于事。

As to that, check on the events 对此,检查事件

  • onhashchange onhashchange
  • onpopstate (be aware IEs implementation thereof is half-baked, even in IE11 --> in my case it didn't respond to mouse interaction, only to js history.*() onpopstate(请注意,即使在IE11中IE的实现也是半熟的->在我的案例中,它不响应鼠标交互,仅响应js历史记录。*()

If you want to catch the user on your site for some hopefully incredibly good purpose: 如果您想在网站上吸引用户是出于某些令人难以置信的良好目的:

  • Create a duplicate entry of your home page on the first homepage-hit via window.history.pushState and instantly window.history.forward() --- (this works especially well and unnoticable on SPAs) 通过window.history.pushState在第一个命中的首页上创建您的主页的重复条目,并立即通过window.history.forward()---(这在SPA上效果特别好且不引人注意)
  • Reiterate that procedure every time the user navigates to your homepage/lowest_level_parent_state ... 每次用户导航到您的主页/ lowest_level_parent_state时,请重复该过程。

Et voila ... In my case I can't even escape our page if I hold down the backspace button ... 等等...就我而言,如果我按下退格键,我什至无法逃脱页面...

Another convenient option would be to put the page into fullscreen mode if feasible :) 另一个方便的选择是,如果可行的话,将页面置于全屏模式:)

Cheers, J 干杯,J

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM