How can I stop the browser back button using JavaScript?

前端 未结 30 3298
没有蜡笔的小新
没有蜡笔的小新 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:55

    None of the most-upvoted answers worked for me in Chrome 79. It looks like Chrome changed its behavior with respect to the Back button after Version 75, see here:

    https://support.google.com/chrome/thread/8721521?hl=en

    However, in that Google thread, the answer provided by Azrulmukmin Azmi at the very end did work. This is his solution.

    
    

    The problem with Chrome is that it doesn't trigger onpopstate event unless you make browser action ( i.e. call history.back). Thats why I've added those to script.

    I don't entirely understand what he wrote, but apparently an additional history.back() / history.forward() is now required for blocking Back in Chrome 75+.

提交回复
热议问题