Trigger an event on back button in browser/android with javascript

Araklaj

My angular app's content creation flow is being broken by androids and browsers physical buttons which take the user to where they came from instead of previous step in the process. I tried fixing it with locationChangeStart as well as few other similar events, but they all get triggered both by my "Continue" buttons as well as physical "back" buttons.

Is there a way to trigger an event only when user presses browsers/android's back button, or alternatively to know if locationChangeStart was triggered by the back button vs app's button? If possible, I would like avoid adding jQuery as we are not currently using it.

Araklaj

I gave up on trying to detect the user pressing back button and act on it. Instead, make it the default action and change the way our buttons behave. I detect when user is pressing one of our buttons and set a variable based on it.

    var navigating = false;

    $scope.$on('$locationChangeStart', function(event) {
        if($scope.global.application="new-ad" && !navigating){
            event.preventDefault();
            $scope.areYouSure = true;
        }
    });
    $scope.nextStep = function() {
        navigating = true;
        $location.url('/step-two');
    }

Basically, we first set our variable to false, and if the user presses physical back, it will display the prompt alerting user they will lose their work. However, if the user instead uses our "continue" button, also triggering the locationChange, it will set variable to true, letting the app know what the locationChange is triggered from within the app and allowing it to continue.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Cordova Angular Android back button does not trigger ng-if

분류에서Dev

How to trigger on click event to a button which is on a modal window

분류에서Dev

Rails 4 Turbolinks back button and multiple event binding because of pagesCached

분류에서Dev

Put focus back on previously focused control on a button click event C# winforms

분류에서Dev

How can we provide default Back key press behaviour to a custom button tap event in Windows Phone 8?

분류에서Dev

trigger event on selected option

분류에서Dev

jquery sortable event trigger

분류에서Dev

Trigger different event on QTimer reset

분류에서Dev

Button Press Event

분류에서Dev

Back button not working in Media player

분류에서Dev

To disable back button in popup window

분류에서Dev

Back Button Does Not Stop Handler

분류에서Dev

ASP.Net submit button click event not working after dropdown list index change in JavaScript

분류에서Dev

Is it bad practice to use trigger() for duplicate event handling?

분류에서Dev

Creating an event to trigger on reminder creation on outlook

분류에서Dev

Is it bad practice to use trigger() for duplicate event handling?

분류에서Dev

Is it bad practice to use trigger() for duplicate event handling?

분류에서Dev

trigger jquery event when page loads

분류에서Dev

Dynamically insert DOM when pageshow event trigger

분류에서Dev

Trigger AngularJS $scope event on key press

분류에서Dev

Trigger event when a link gets visited

분류에서Dev

Trigger Event automatically on DOMCharacterChanged or change on div

분류에서Dev

jquery click event won't trigger

분류에서Dev

Manually trigger mouseover event without allowing the event to propagate

분류에서Dev

Trigger event on div's content which has event

분류에서Dev

GTK : Any way to mximize the window on Button trigger

분류에서Dev

Click Event UI button jQuery

분류에서Dev

.onAppear is calling when I navigated back to a view by clicking back button

분류에서Dev

HOWTO - trigger source button in ckeditor by another button which is outside the editor

Related 관련 기사

  1. 1

    Cordova Angular Android back button does not trigger ng-if

  2. 2

    How to trigger on click event to a button which is on a modal window

  3. 3

    Rails 4 Turbolinks back button and multiple event binding because of pagesCached

  4. 4

    Put focus back on previously focused control on a button click event C# winforms

  5. 5

    How can we provide default Back key press behaviour to a custom button tap event in Windows Phone 8?

  6. 6

    trigger event on selected option

  7. 7

    jquery sortable event trigger

  8. 8

    Trigger different event on QTimer reset

  9. 9

    Button Press Event

  10. 10

    Back button not working in Media player

  11. 11

    To disable back button in popup window

  12. 12

    Back Button Does Not Stop Handler

  13. 13

    ASP.Net submit button click event not working after dropdown list index change in JavaScript

  14. 14

    Is it bad practice to use trigger() for duplicate event handling?

  15. 15

    Creating an event to trigger on reminder creation on outlook

  16. 16

    Is it bad practice to use trigger() for duplicate event handling?

  17. 17

    Is it bad practice to use trigger() for duplicate event handling?

  18. 18

    trigger jquery event when page loads

  19. 19

    Dynamically insert DOM when pageshow event trigger

  20. 20

    Trigger AngularJS $scope event on key press

  21. 21

    Trigger event when a link gets visited

  22. 22

    Trigger Event automatically on DOMCharacterChanged or change on div

  23. 23

    jquery click event won't trigger

  24. 24

    Manually trigger mouseover event without allowing the event to propagate

  25. 25

    Trigger event on div's content which has event

  26. 26

    GTK : Any way to mximize the window on Button trigger

  27. 27

    Click Event UI button jQuery

  28. 28

    .onAppear is calling when I navigated back to a view by clicking back button

  29. 29

    HOWTO - trigger source button in ckeditor by another button which is outside the editor

뜨겁다태그

보관