Forward button disable
JavaScript-Angular2
by Eugen Sunic
JavaScript
// BEGIN
// variables for forward button
cnt = false;
arrayHistory = [];
popstate = false;
constructor(private translate: TranslateService,
private location: PlatformLocation,
private router: Router,
private messageService: EnvironmentServiceSecond) {
this.router.events
.filter(event => event instanceof NavigationEnd)
.subscribe(event => {
if (!this.popstate) {
this.arrayHistory.push(window.location.hash);
this.cnt = false;
}
this.popstate = false;
});
this.location.onPopState((e) => {
const hashValue = '#/';
const previousPage = -1;
if (!this.cnt) {
this.popstate = true;
this.arrayHistory.pop();
const lastEntry = this.arrayHistory[this.arrayHistory.length - 1];
if (lastEntry !== undefined) {
window.history.pushState(null, null, lastEntry);
this.cnt = true;
} else {
if (window.location.hash !== hashValue) {
this.cnt = true;
window.history.pushState(null, null, window.location.hash);
} else {
window.history.pushState(null, null, '/'.concat(hashValue));
}
}
} else {
this.cnt = false;
window.history.go(previousPage);
}
if (typeof this.arrayHistory[this.arrayHistory.length - 1] === 'undefined' && window.location.hash === hashValue) {
this.cnt = true;
this.arrayHistory = [];
}
});
}
// END