我正在用PHP做一个在线测试应用程序。我想限制用户在考试中返回。

我尝试了下面的脚本,但它停止了我的计时器。

我该怎么办?

定时器存储在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值中获取考试的持续时间。计时器随之启动,但当我输入禁用后退按钮的代码时,它就停止了。我有什么问题?


当前回答

轻松尝试一下:

history.pushState(null, null, document.title);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.title);
});

其他回答

有很多原因可以解释为什么禁用后退按钮实际上不起作用。最好的办法是警告用户:

window.onbeforeunload = function() { return "Your work will be lost."; };

本页确实列出了一些禁用后退按钮的方法,但都不保证:

http://www.irt.org/script/311.htm

这段代码在最新的Chrome和Firefox浏览器上进行了测试。

<script type="text/javascript">
    history.pushState(null, null, location.href);
    history.back();
    history.forward();
    window.onpopstate = function () { history.go(1); };
</script>
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});

此JavaScript代码不允许任何用户返回(适用于Chrome, Firefox, Internet Explorer和Edge)。

如何阻止反向功能:

历史。pushState(null, null, location.href); 窗口。Onpopstate = function () { history.go (1); };

这似乎在禁用浏览器上的后退按钮,以及退格键带你回到工作。

history.pushState(null, null, $(location).attr('href'));
window.addEventListener('popstate', function () {
    history.pushState(null, null, $(location).attr('href'));
});