Angular2+ | Remove forward history after browsing back












1















To be able to close my (dynamically added) modal with the browser back button, I am adding a #modal hash to my url once a modal opens. After the browser back button is pressed I listen to the pop state, check if there is a modal open and remove it. When the modal is closed without using the browser back button, I perform a history.back() and remove the modal. The side effect is that there is a forward path left in the history, which is the #modal hash.



Is there a way to get rid of this forward history?





I tried to navigate to the same URL after navigating back but this doesn't seem to do anything. I am also using queryParams in my app but I do not think this affects anything here.



When opening the modal, I add #modal:



this.router.navigate(, {fragment: 'modal', queryParamsHandling: 'merge'})


On pop state:



location.onPopState( () => {
if(this.modalCount > 0){
this.removeAllModals();
const routerSub = this.router.events.subscribe(event => {
if(event instanceof NavigationEnd) {
this.router.navigate(, {replaceUrl:true, queryParamsHandling: 'merge';
routerSub.unsubscribe();
}
})
}
})









share|improve this question



























    1















    To be able to close my (dynamically added) modal with the browser back button, I am adding a #modal hash to my url once a modal opens. After the browser back button is pressed I listen to the pop state, check if there is a modal open and remove it. When the modal is closed without using the browser back button, I perform a history.back() and remove the modal. The side effect is that there is a forward path left in the history, which is the #modal hash.



    Is there a way to get rid of this forward history?





    I tried to navigate to the same URL after navigating back but this doesn't seem to do anything. I am also using queryParams in my app but I do not think this affects anything here.



    When opening the modal, I add #modal:



    this.router.navigate(, {fragment: 'modal', queryParamsHandling: 'merge'})


    On pop state:



    location.onPopState( () => {
    if(this.modalCount > 0){
    this.removeAllModals();
    const routerSub = this.router.events.subscribe(event => {
    if(event instanceof NavigationEnd) {
    this.router.navigate(, {replaceUrl:true, queryParamsHandling: 'merge';
    routerSub.unsubscribe();
    }
    })
    }
    })









    share|improve this question

























      1












      1








      1








      To be able to close my (dynamically added) modal with the browser back button, I am adding a #modal hash to my url once a modal opens. After the browser back button is pressed I listen to the pop state, check if there is a modal open and remove it. When the modal is closed without using the browser back button, I perform a history.back() and remove the modal. The side effect is that there is a forward path left in the history, which is the #modal hash.



      Is there a way to get rid of this forward history?





      I tried to navigate to the same URL after navigating back but this doesn't seem to do anything. I am also using queryParams in my app but I do not think this affects anything here.



      When opening the modal, I add #modal:



      this.router.navigate(, {fragment: 'modal', queryParamsHandling: 'merge'})


      On pop state:



      location.onPopState( () => {
      if(this.modalCount > 0){
      this.removeAllModals();
      const routerSub = this.router.events.subscribe(event => {
      if(event instanceof NavigationEnd) {
      this.router.navigate(, {replaceUrl:true, queryParamsHandling: 'merge';
      routerSub.unsubscribe();
      }
      })
      }
      })









      share|improve this question














      To be able to close my (dynamically added) modal with the browser back button, I am adding a #modal hash to my url once a modal opens. After the browser back button is pressed I listen to the pop state, check if there is a modal open and remove it. When the modal is closed without using the browser back button, I perform a history.back() and remove the modal. The side effect is that there is a forward path left in the history, which is the #modal hash.



      Is there a way to get rid of this forward history?





      I tried to navigate to the same URL after navigating back but this doesn't seem to do anything. I am also using queryParams in my app but I do not think this affects anything here.



      When opening the modal, I add #modal:



      this.router.navigate(, {fragment: 'modal', queryParamsHandling: 'merge'})


      On pop state:



      location.onPopState( () => {
      if(this.modalCount > 0){
      this.removeAllModals();
      const routerSub = this.router.events.subscribe(event => {
      if(event instanceof NavigationEnd) {
      this.router.navigate(, {replaceUrl:true, queryParamsHandling: 'merge';
      routerSub.unsubscribe();
      }
      })
      }
      })






      angular hash modal-dialog history forward






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 11:55









      kingofbbqkingofbbq

      2016




      2016
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Use



          navigateByUrl()




          Navigate based on the provided url. This navigation is always
          absolute.




          It will replace the entire Url by defined path



          location.onPopState( () => {
          if(this.modalCount > 0){
          this.removeAllModals();
          this.router.events.subscribe(event => {
          if(event instanceof NavigationEnd) {
          this.router.navigateByUrl('');
          }
          })
          }
          })





          share|improve this answer





















          • 1





            I was wondering this if you do routerSub.unsubscribe(); doesn't it mean you can't go backwards after you tried a forward?

            – Swoox
            Nov 23 '18 at 12:09











          • yep changed the code

            – Chellappan
            Nov 23 '18 at 12:13











          • Not sure if I am doing something wrong, but this does not solve the issue for me. Without unsubscribing, navigation completely breaks, because it keeps loading the same page on every navigation event. With unsubscribing, it adds another state to the history, ending up with 2 times the same state. And when I add replaceURL it does not remove the forward state. Also I noticed with this method, the entire page reloads, which is not acceptable in my case because a lot of data has to reload.

            – kingofbbq
            Nov 23 '18 at 14:52











          • why are you using events.subscribe?

            – Chellappan
            Nov 23 '18 at 15:19











          • Good question, I thought I had to wait until navigationEnd before doing another 'navigate'.

            – kingofbbq
            Nov 23 '18 at 16:13











          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%2f53446263%2fangular2-remove-forward-history-after-browsing-back%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Use



          navigateByUrl()




          Navigate based on the provided url. This navigation is always
          absolute.




          It will replace the entire Url by defined path



          location.onPopState( () => {
          if(this.modalCount > 0){
          this.removeAllModals();
          this.router.events.subscribe(event => {
          if(event instanceof NavigationEnd) {
          this.router.navigateByUrl('');
          }
          })
          }
          })





          share|improve this answer





















          • 1





            I was wondering this if you do routerSub.unsubscribe(); doesn't it mean you can't go backwards after you tried a forward?

            – Swoox
            Nov 23 '18 at 12:09











          • yep changed the code

            – Chellappan
            Nov 23 '18 at 12:13











          • Not sure if I am doing something wrong, but this does not solve the issue for me. Without unsubscribing, navigation completely breaks, because it keeps loading the same page on every navigation event. With unsubscribing, it adds another state to the history, ending up with 2 times the same state. And when I add replaceURL it does not remove the forward state. Also I noticed with this method, the entire page reloads, which is not acceptable in my case because a lot of data has to reload.

            – kingofbbq
            Nov 23 '18 at 14:52











          • why are you using events.subscribe?

            – Chellappan
            Nov 23 '18 at 15:19











          • Good question, I thought I had to wait until navigationEnd before doing another 'navigate'.

            – kingofbbq
            Nov 23 '18 at 16:13
















          1














          Use



          navigateByUrl()




          Navigate based on the provided url. This navigation is always
          absolute.




          It will replace the entire Url by defined path



          location.onPopState( () => {
          if(this.modalCount > 0){
          this.removeAllModals();
          this.router.events.subscribe(event => {
          if(event instanceof NavigationEnd) {
          this.router.navigateByUrl('');
          }
          })
          }
          })





          share|improve this answer





















          • 1





            I was wondering this if you do routerSub.unsubscribe(); doesn't it mean you can't go backwards after you tried a forward?

            – Swoox
            Nov 23 '18 at 12:09











          • yep changed the code

            – Chellappan
            Nov 23 '18 at 12:13











          • Not sure if I am doing something wrong, but this does not solve the issue for me. Without unsubscribing, navigation completely breaks, because it keeps loading the same page on every navigation event. With unsubscribing, it adds another state to the history, ending up with 2 times the same state. And when I add replaceURL it does not remove the forward state. Also I noticed with this method, the entire page reloads, which is not acceptable in my case because a lot of data has to reload.

            – kingofbbq
            Nov 23 '18 at 14:52











          • why are you using events.subscribe?

            – Chellappan
            Nov 23 '18 at 15:19











          • Good question, I thought I had to wait until navigationEnd before doing another 'navigate'.

            – kingofbbq
            Nov 23 '18 at 16:13














          1












          1








          1







          Use



          navigateByUrl()




          Navigate based on the provided url. This navigation is always
          absolute.




          It will replace the entire Url by defined path



          location.onPopState( () => {
          if(this.modalCount > 0){
          this.removeAllModals();
          this.router.events.subscribe(event => {
          if(event instanceof NavigationEnd) {
          this.router.navigateByUrl('');
          }
          })
          }
          })





          share|improve this answer















          Use



          navigateByUrl()




          Navigate based on the provided url. This navigation is always
          absolute.




          It will replace the entire Url by defined path



          location.onPopState( () => {
          if(this.modalCount > 0){
          this.removeAllModals();
          this.router.events.subscribe(event => {
          if(event instanceof NavigationEnd) {
          this.router.navigateByUrl('');
          }
          })
          }
          })






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 12:13

























          answered Nov 23 '18 at 12:00









          ChellappanChellappan

          5,3192419




          5,3192419








          • 1





            I was wondering this if you do routerSub.unsubscribe(); doesn't it mean you can't go backwards after you tried a forward?

            – Swoox
            Nov 23 '18 at 12:09











          • yep changed the code

            – Chellappan
            Nov 23 '18 at 12:13











          • Not sure if I am doing something wrong, but this does not solve the issue for me. Without unsubscribing, navigation completely breaks, because it keeps loading the same page on every navigation event. With unsubscribing, it adds another state to the history, ending up with 2 times the same state. And when I add replaceURL it does not remove the forward state. Also I noticed with this method, the entire page reloads, which is not acceptable in my case because a lot of data has to reload.

            – kingofbbq
            Nov 23 '18 at 14:52











          • why are you using events.subscribe?

            – Chellappan
            Nov 23 '18 at 15:19











          • Good question, I thought I had to wait until navigationEnd before doing another 'navigate'.

            – kingofbbq
            Nov 23 '18 at 16:13














          • 1





            I was wondering this if you do routerSub.unsubscribe(); doesn't it mean you can't go backwards after you tried a forward?

            – Swoox
            Nov 23 '18 at 12:09











          • yep changed the code

            – Chellappan
            Nov 23 '18 at 12:13











          • Not sure if I am doing something wrong, but this does not solve the issue for me. Without unsubscribing, navigation completely breaks, because it keeps loading the same page on every navigation event. With unsubscribing, it adds another state to the history, ending up with 2 times the same state. And when I add replaceURL it does not remove the forward state. Also I noticed with this method, the entire page reloads, which is not acceptable in my case because a lot of data has to reload.

            – kingofbbq
            Nov 23 '18 at 14:52











          • why are you using events.subscribe?

            – Chellappan
            Nov 23 '18 at 15:19











          • Good question, I thought I had to wait until navigationEnd before doing another 'navigate'.

            – kingofbbq
            Nov 23 '18 at 16:13








          1




          1





          I was wondering this if you do routerSub.unsubscribe(); doesn't it mean you can't go backwards after you tried a forward?

          – Swoox
          Nov 23 '18 at 12:09





          I was wondering this if you do routerSub.unsubscribe(); doesn't it mean you can't go backwards after you tried a forward?

          – Swoox
          Nov 23 '18 at 12:09













          yep changed the code

          – Chellappan
          Nov 23 '18 at 12:13





          yep changed the code

          – Chellappan
          Nov 23 '18 at 12:13













          Not sure if I am doing something wrong, but this does not solve the issue for me. Without unsubscribing, navigation completely breaks, because it keeps loading the same page on every navigation event. With unsubscribing, it adds another state to the history, ending up with 2 times the same state. And when I add replaceURL it does not remove the forward state. Also I noticed with this method, the entire page reloads, which is not acceptable in my case because a lot of data has to reload.

          – kingofbbq
          Nov 23 '18 at 14:52





          Not sure if I am doing something wrong, but this does not solve the issue for me. Without unsubscribing, navigation completely breaks, because it keeps loading the same page on every navigation event. With unsubscribing, it adds another state to the history, ending up with 2 times the same state. And when I add replaceURL it does not remove the forward state. Also I noticed with this method, the entire page reloads, which is not acceptable in my case because a lot of data has to reload.

          – kingofbbq
          Nov 23 '18 at 14:52













          why are you using events.subscribe?

          – Chellappan
          Nov 23 '18 at 15:19





          why are you using events.subscribe?

          – Chellappan
          Nov 23 '18 at 15:19













          Good question, I thought I had to wait until navigationEnd before doing another 'navigate'.

          – kingofbbq
          Nov 23 '18 at 16:13





          Good question, I thought I had to wait until navigationEnd before doing another 'navigate'.

          – kingofbbq
          Nov 23 '18 at 16:13




















          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%2f53446263%2fangular2-remove-forward-history-after-browsing-back%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

          Ottavio Pratesi

          Tricia Helfer

          15 giugno