简体   繁体   中英

trigger on event onpopstate on load chrome

I am dealing with a problem that disable back button on browser, so my approach is using onpopstate event, When page load success, then I click back button on browser, it's not trigger on onpopstate event and still can navigate to previous page, if I do anything in page such as left, right click on page then click back button on browser, it trigger on onpopstate event and prevent back to previous page. this issue just happened on chrome, safari works fine. I tried to many implementation, but it's still not work on chrome browser. Has anyone faced this problem yet?

ngOnInit(){
   history.pushState(null, null, window.location.href);
   window.onpopstate = ()=>{
       history.go(1);
   }
}

It's possible to use PlatformLocation which has onPopState listener.

import { PlatformLocation } from '@angular/common'

(...)

constructor(location: PlatformLocation) {

    location.onPopState(() => {

        console.log('pressed back!');

    });

}

(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM