How can I stop the browser back button using JavaScript?

前端 未结 30 4088
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 00:07

I am doing an online quiz application in PHP. I want to restrict the user from going back in an exam.

I have tried the following script, but it stops my timer.

30条回答
  •  生来不讨喜
    2020-11-22 00:48

    This is the way I could it accomplish it. Weirdly changing the window.location didn't worked out fine in chrome and safari. Happens that the location.hash doesn't create an entry in the history for chrome and safari. So you will have to use the pushstate. This is working for me in all browsers.

        history.pushState({ page: 1 }, "title 1", "#nbb");
        window.onhashchange = function (event) {
            window.location.hash = "nbb";
    
        };
    

提交回复
热议问题