如何使用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 中.
<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屋!