Disable back-page button in browser












0















I create auth app with routing in angular 2 with typescript. And now my task is disabled back button in browser from form localhost/form1 to localhost/login; how implement this?










share|improve this question



























    0















    I create auth app with routing in angular 2 with typescript. And now my task is disabled back button in browser from form localhost/form1 to localhost/login; how implement this?










    share|improve this question

























      0












      0








      0


      1






      I create auth app with routing in angular 2 with typescript. And now my task is disabled back button in browser from form localhost/form1 to localhost/login; how implement this?










      share|improve this question














      I create auth app with routing in angular 2 with typescript. And now my task is disabled back button in browser from form localhost/form1 to localhost/login; how implement this?







      angular angular2-routing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 8 '16 at 9:14









      eduard eduard

      4228




      4228
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You can't really block the back button since it's a browser behavior. What you can do instead is to create a guard to handle every request, and if the request is done to load /login and the user is already loggued, simply redirect to the previous page.






          share|improve this answer
























          • Thanks for quick answer, i add something that you tell, i mean AuthGuard. But not for back page. Thanks for response.

            – eduard
            Dec 8 '16 at 9:46





















          0














          I solved this in my angular cordova app to prevent app from exiting as following. Contrary to most comments I've found I had to use the "document" object instead of the "window" object to capture the backbutton event



          // Prevent from exiting app by hook into document backbutton
          @HostListener('document:backbutton', ['$event'])
          onPopState(event) {
          alert('Start page');
          }


          Usage in a component



          import { Component, HostListener  } from '@angular/core';

          export class myComponent {
          constructor() {}

          // Prevent from exiting app by hook into document backbutton
          @HostListener('document:backbutton', ['$event'])
          onPopState(event) {
          alert('Start page');
          }
          }





          share|improve this answer































            -1














            step 1: Import Locatoion from angular commmon



            import {Location} from "@angular/common";


            step 2: Initialise in constructor



            private commonService: CommonService


            step 3: Add function in ngOnInit of the respective coponent,



            this.location.subscribe(currentLocation => {
            if (currentLocation.url === '*/basic-info*') {
            window.onpopstate = function (event) {
            history.go(1);
            }
            }


            });



            Note: Here /basic-info will be replaced by your path.



            If first time it is not working, try adding outside subscribe,



            let currentUrl = window.location.href;
            let tmpVar = currentUrl.includes('/basic-info');
            if (currentUrl.includes('/basic-info')) {
            window.onpopstate = function (event) {
            history.go(1);
            }
            }





            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41035672%2fdisable-back-page-button-in-browser%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You can't really block the back button since it's a browser behavior. What you can do instead is to create a guard to handle every request, and if the request is done to load /login and the user is already loggued, simply redirect to the previous page.






              share|improve this answer
























              • Thanks for quick answer, i add something that you tell, i mean AuthGuard. But not for back page. Thanks for response.

                – eduard
                Dec 8 '16 at 9:46


















              1














              You can't really block the back button since it's a browser behavior. What you can do instead is to create a guard to handle every request, and if the request is done to load /login and the user is already loggued, simply redirect to the previous page.






              share|improve this answer
























              • Thanks for quick answer, i add something that you tell, i mean AuthGuard. But not for back page. Thanks for response.

                – eduard
                Dec 8 '16 at 9:46
















              1












              1








              1







              You can't really block the back button since it's a browser behavior. What you can do instead is to create a guard to handle every request, and if the request is done to load /login and the user is already loggued, simply redirect to the previous page.






              share|improve this answer













              You can't really block the back button since it's a browser behavior. What you can do instead is to create a guard to handle every request, and if the request is done to load /login and the user is already loggued, simply redirect to the previous page.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 8 '16 at 9:18









              SakutoSakuto

              3,48512542




              3,48512542













              • Thanks for quick answer, i add something that you tell, i mean AuthGuard. But not for back page. Thanks for response.

                – eduard
                Dec 8 '16 at 9:46





















              • Thanks for quick answer, i add something that you tell, i mean AuthGuard. But not for back page. Thanks for response.

                – eduard
                Dec 8 '16 at 9:46



















              Thanks for quick answer, i add something that you tell, i mean AuthGuard. But not for back page. Thanks for response.

              – eduard
              Dec 8 '16 at 9:46







              Thanks for quick answer, i add something that you tell, i mean AuthGuard. But not for back page. Thanks for response.

              – eduard
              Dec 8 '16 at 9:46















              0














              I solved this in my angular cordova app to prevent app from exiting as following. Contrary to most comments I've found I had to use the "document" object instead of the "window" object to capture the backbutton event



              // Prevent from exiting app by hook into document backbutton
              @HostListener('document:backbutton', ['$event'])
              onPopState(event) {
              alert('Start page');
              }


              Usage in a component



              import { Component, HostListener  } from '@angular/core';

              export class myComponent {
              constructor() {}

              // Prevent from exiting app by hook into document backbutton
              @HostListener('document:backbutton', ['$event'])
              onPopState(event) {
              alert('Start page');
              }
              }





              share|improve this answer




























                0














                I solved this in my angular cordova app to prevent app from exiting as following. Contrary to most comments I've found I had to use the "document" object instead of the "window" object to capture the backbutton event



                // Prevent from exiting app by hook into document backbutton
                @HostListener('document:backbutton', ['$event'])
                onPopState(event) {
                alert('Start page');
                }


                Usage in a component



                import { Component, HostListener  } from '@angular/core';

                export class myComponent {
                constructor() {}

                // Prevent from exiting app by hook into document backbutton
                @HostListener('document:backbutton', ['$event'])
                onPopState(event) {
                alert('Start page');
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  I solved this in my angular cordova app to prevent app from exiting as following. Contrary to most comments I've found I had to use the "document" object instead of the "window" object to capture the backbutton event



                  // Prevent from exiting app by hook into document backbutton
                  @HostListener('document:backbutton', ['$event'])
                  onPopState(event) {
                  alert('Start page');
                  }


                  Usage in a component



                  import { Component, HostListener  } from '@angular/core';

                  export class myComponent {
                  constructor() {}

                  // Prevent from exiting app by hook into document backbutton
                  @HostListener('document:backbutton', ['$event'])
                  onPopState(event) {
                  alert('Start page');
                  }
                  }





                  share|improve this answer













                  I solved this in my angular cordova app to prevent app from exiting as following. Contrary to most comments I've found I had to use the "document" object instead of the "window" object to capture the backbutton event



                  // Prevent from exiting app by hook into document backbutton
                  @HostListener('document:backbutton', ['$event'])
                  onPopState(event) {
                  alert('Start page');
                  }


                  Usage in a component



                  import { Component, HostListener  } from '@angular/core';

                  export class myComponent {
                  constructor() {}

                  // Prevent from exiting app by hook into document backbutton
                  @HostListener('document:backbutton', ['$event'])
                  onPopState(event) {
                  alert('Start page');
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 9 '17 at 6:46









                  KarlKarl

                  2,13321522




                  2,13321522























                      -1














                      step 1: Import Locatoion from angular commmon



                      import {Location} from "@angular/common";


                      step 2: Initialise in constructor



                      private commonService: CommonService


                      step 3: Add function in ngOnInit of the respective coponent,



                      this.location.subscribe(currentLocation => {
                      if (currentLocation.url === '*/basic-info*') {
                      window.onpopstate = function (event) {
                      history.go(1);
                      }
                      }


                      });



                      Note: Here /basic-info will be replaced by your path.



                      If first time it is not working, try adding outside subscribe,



                      let currentUrl = window.location.href;
                      let tmpVar = currentUrl.includes('/basic-info');
                      if (currentUrl.includes('/basic-info')) {
                      window.onpopstate = function (event) {
                      history.go(1);
                      }
                      }





                      share|improve this answer




























                        -1














                        step 1: Import Locatoion from angular commmon



                        import {Location} from "@angular/common";


                        step 2: Initialise in constructor



                        private commonService: CommonService


                        step 3: Add function in ngOnInit of the respective coponent,



                        this.location.subscribe(currentLocation => {
                        if (currentLocation.url === '*/basic-info*') {
                        window.onpopstate = function (event) {
                        history.go(1);
                        }
                        }


                        });



                        Note: Here /basic-info will be replaced by your path.



                        If first time it is not working, try adding outside subscribe,



                        let currentUrl = window.location.href;
                        let tmpVar = currentUrl.includes('/basic-info');
                        if (currentUrl.includes('/basic-info')) {
                        window.onpopstate = function (event) {
                        history.go(1);
                        }
                        }





                        share|improve this answer


























                          -1












                          -1








                          -1







                          step 1: Import Locatoion from angular commmon



                          import {Location} from "@angular/common";


                          step 2: Initialise in constructor



                          private commonService: CommonService


                          step 3: Add function in ngOnInit of the respective coponent,



                          this.location.subscribe(currentLocation => {
                          if (currentLocation.url === '*/basic-info*') {
                          window.onpopstate = function (event) {
                          history.go(1);
                          }
                          }


                          });



                          Note: Here /basic-info will be replaced by your path.



                          If first time it is not working, try adding outside subscribe,



                          let currentUrl = window.location.href;
                          let tmpVar = currentUrl.includes('/basic-info');
                          if (currentUrl.includes('/basic-info')) {
                          window.onpopstate = function (event) {
                          history.go(1);
                          }
                          }





                          share|improve this answer













                          step 1: Import Locatoion from angular commmon



                          import {Location} from "@angular/common";


                          step 2: Initialise in constructor



                          private commonService: CommonService


                          step 3: Add function in ngOnInit of the respective coponent,



                          this.location.subscribe(currentLocation => {
                          if (currentLocation.url === '*/basic-info*') {
                          window.onpopstate = function (event) {
                          history.go(1);
                          }
                          }


                          });



                          Note: Here /basic-info will be replaced by your path.



                          If first time it is not working, try adding outside subscribe,



                          let currentUrl = window.location.href;
                          let tmpVar = currentUrl.includes('/basic-info');
                          if (currentUrl.includes('/basic-info')) {
                          window.onpopstate = function (event) {
                          history.go(1);
                          }
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 11 '18 at 16:57









                          saravana vasaravana va

                          261312




                          261312






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41035672%2fdisable-back-page-button-in-browser%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                              Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                              A Topological Invariant for $pi_3(U(n))$