disable forward button

forward button in JavaScript

by Eugen Sunic

JavaScript

//-------
  cnt = false;
  cnt2 = false;
  just = false;
  second_back = false;
  //-------
  message: any;
  popstate = false;
  default_page = '';
  subscription: Subscription;

  constructor(private translate: TranslateService, private location: PlatformLocation, private router: Router, private messageService: EnvironmentServiceSecond) {
      // introduce
      this.default_page = window.location.href;
      //event routing everythime we click on it or type in URL
      this.router.events
        .filter(event => event instanceof NavigationEnd)
        .subscribe(event => {
          if (!this.popstate) {
            this.cnt = this.cnt2 = this.just = this.second_back = false;
          }
          this.popstate = false;
        });

      this.location.onPopState((e) => {
        if (window.history.state === this.default_page) {
          window.history.back();
        }
        this.popstate = true;
        if (!this.cnt) {
          this.cnt = true;

          if (this.just) {
            this.second_back = this.cnt = true;
            this.just = false;
            window.history.back();
          }
          if (this.cnt2) {
            this.cnt = this.cnt2 = false;
            this.just = true;
          }
          window.history.pushState(this.default_page, null, window.location.hash);

        } else if (this.cnt && !this.second_back) {
          this.cnt = false;
          this.cnt2 = true;
          window.history.back();

        } else if (this.second_back) {
          this.second_back = false;
          window.history.pushState(this.default_page, null, window.location.hash);
        }

      });