如何使用 JavaScript 停止浏览器后退按钮? [英] How can I stop the browser back button using JavaScript?
问题描述
我正在用 PHP 做一个在线测验应用程序.我想限制用户返回考试.
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.
我该怎么办?
计时器存储在文件 cdtimer.js 中.
The timer is stored in file cdtimer.js.
<script type="text/javascript">
window.history.forward();
function noBack()
{
window.history.forward();
}
</script>
<body onLoad="noBack();" onpageshow="if (event.persisted) noBack();" onUnload="">
我有一个考试计时器,它从 MySQL 值中获取考试的持续时间.计时器相应地启动,但是当我输入禁用后退按钮的代码时它会停止.我的问题是什么?
I have the exam timer which takes a duration for the exam from a MySQL value. The timer starts accordingly, but it stops when I put the code in for disabling the back button. What is my problem?
推荐答案
禁用后退按钮实际上不起作用的原因有很多.最好的办法是警告用户:
There are numerous reasons why disabling the back button will not really work. Your best bet is to warn the user:
window.onbeforeunload = function() { return "Your work will be lost."; };
此页面确实列出了您可以尝试禁用后退按钮的多种方法,但不能保证:
This page does list a number of ways you could try to disable the back button, but none are guaranteed:
http://www.irt.org/script/311.htm
这篇关于如何使用 JavaScript 停止浏览器后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!