3

enter image description here

enter image description here

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 .

1
  • Is it really painful for you to review the answers? Commented Jan 13, 2017 at 12:12

4 Answers 4

7

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();            
});
Sign up to request clarification or add additional context in comments.

4 Comments

very nice, so simple!
@sana, It works for me without any issues. It is very simple logic. Can you please check the code once again?
it does not work for me, I am using ui-rouer with html5mode set to true.
this just disables navigation completely, I don't see how it solved the problem.
1

It's not a very straight forward thing in Angular.js but you can use JavaScript function - window.onhashchange to disable to back button.

Take a look at this link, you might get better ideas.

Comments

0

Use javascript event

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

refer

Comments

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.

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
  • 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.*()

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)
  • Reiterate that procedure every time the user navigates to your homepage/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

2 Comments

can you elaborate on your solution with more code? it is very ambiguous and I can't understand what you really mean.
1.) on landing page of your site, add a script that uses window.history.pushState to push a duplicate of the index page 2.) instantly call window.history.forward as to have one spare history item of your own page in the history 3.) repeat process everytime somebody hits the landing page (example via the back button) Sorry, can't really remember nor access the code anymore, all I can say, this really works, it's not angular specific though. Don't have time now to come up with new code, please read the manual :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.