How to Detect Browser Back Button event - Cross Browser

后端 未结 16 2728
鱼传尺愫
鱼传尺愫 2020-11-21 23:09

How do you definitively detect whether or not the user has pressed the back button in the browser?

How do you enforce the use of an in-page back button inside a sin

16条回答
  •  灰色年华
    2020-11-22 00:05

    I was able to use some of the answers in this thread and others to get it working in IE and Chrome/Edge. history.pushState for me wasn't supported in IE11.

    if (history.pushState) {
        //Chrome and modern browsers
        history.pushState(null, document.title, location.href);
        window.addEventListener('popstate', function (event) {
            history.pushState(null, document.title, location.href);
        });
    }
    else {
        //IE
        history.forward();
    }
    

提交回复
热议问题