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

时间:2023-01-21 14:42:34

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

如何在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 .

我想阻止浏览器返回并刷新。请回答 。

4 个解决方案

#1


4  

If you want disable browser back button in angular, please try out the following code. It may help.

如果您想要以角度方式禁用浏览器后退按钮,请尝试以下代码。它可能有所帮助。

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

#2


0  

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.

看看这个链接,你可能会得到更好的想法。

#3


0  

Use javascript event

使用javascript事件

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

refer

参考

#4


0  

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(请注意IE的实现是半生不熟的,即使在IE11中 - >在我的情况下它没有响应鼠标交互,只对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 ...

Et瞧...在我的情况下,如果我按住退格按钮,我甚至无法逃脱我们的页面...

Another convenient option would be to put the page into fullscreen mode if feasible :)

另一个方便的选择是在可行的情况下将页面置于全屏模式:)

Cheers, J

干杯,J

#1


4  

If you want disable browser back button in angular, please try out the following code. It may help.

如果您想要以角度方式禁用浏览器后退按钮,请尝试以下代码。它可能有所帮助。

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

#2


0  

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.

看看这个链接,你可能会得到更好的想法。

#3


0  

Use javascript event

使用javascript事件

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

refer

参考

#4


0  

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(请注意IE的实现是半生不熟的,即使在IE11中 - >在我的情况下它没有响应鼠标交互,只对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 ...

Et瞧...在我的情况下,如果我按住退格按钮,我甚至无法逃脱我们的页面...

Another convenient option would be to put the page into fullscreen mode if feasible :)

另一个方便的选择是在可行的情况下将页面置于全屏模式:)

Cheers, J

干杯,J