我正在用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值中获取考试的持续时间。计时器随之启动,但当我输入禁用后退按钮的代码时,它就停止了。我有什么问题?
This code will disable the back button for modern browsers which support the HTML5 History API. Under normal circumstances, pushing the back button goes back one step, to the previous page. If you use history.pushState(), you start adding extra sub-steps to the current page. The way it works is, if you were to use history.pushState() three times, then start pushing the back button, the first three times it would navigate back in these sub-steps, and then the fourth time it would go back to the previous page.
如果您将此行为与popstate事件上的事件侦听器结合在一起,您实际上可以建立一个无限循环的子状态。因此,您加载页面,按下一个子状态,然后点击返回按钮,弹出一个子状态,并按下另一个子状态,因此,如果您再次按下返回按钮,它将永远不会没有子状态可按。如果你觉得有必要禁用后退按钮,这将使你达到目的。
history.pushState(null, null, 'no-back-button');
window.addEventListener('popstate', function(event) {
history.pushState(null, null, 'no-back-button');
});
非常简单和干净的函数打破向后箭头,而不干扰页面之后。
好处:
Loads instantaneously and restores original hash, so the user isn't distracted by URL visibly changing.
The user can still exit by pressing back 10 times (that's a good thing), but not accidentally
No user interference like other solutions using onbeforeunload
It only runs once and doesn't interfere with further hash manipulations in case you use that to track state
Restores original hash, so almost invisible.
Uses setInterval, so it doesn't break slow browsers and always works.
Pure JavaScript, does not require HTML5 history, works everywhere.
Unobtrusive, simple, and plays well with other code.
Does not use unbeforeunload which interrupts user with modal dialog.
It just works without fuss.
注意:其他一些解决方案使用onbeforeunload。请不要为此目的使用onbeforeunload,当用户试图关闭窗口、点击反向箭头等时,onbeforeunload会弹出一个对话框。像onbeforeunload这样的情态动词通常只适用于很少的情况,比如当它们实际上在屏幕上做了更改并且没有保存它们时,不是为了这个目的。
工作原理
在页面加载时执行
保存您的原始散列(如果URL中有一个)。
依次追加#/noop/{1..10}到哈希
恢复原始哈希
就是这样。没有更多的混乱,没有后台事件监控,没有其他。
在一秒钟内使用它
要部署,只需在页面或JavaScript代码中添加这个:
<script>
/* Break back button */
window.onload = function(){
var i = 0;
var previous_hash = window.location.hash;
var x = setInterval(function(){
i++;
window.location.hash = "/noop/" + i;
if (i==10){
clearInterval(x);
window.location.hash = previous_hash;
}
}, 10);
}
</script>
重写web浏览器的默认行为通常不是一个好主意。出于安全原因,客户端脚本没有足够的特权来执行此操作。
还有一些类似的问题,
如何防止退格键导航回?
我如何可以防止浏览器的默认历史后退动作退格按钮与JavaScript?
你不能禁用浏览器的后退按钮。然而,你可以使用你的逻辑来阻止用户返回,这将创造一个像它被禁用的印象。这是如何-检查以下片段。
(function (global) {
if(typeof (global) === "undefined") {
throw new Error("window is undefined");
}
var _hash = "!";
var noBackPlease = function () {
global.location.href += "#";
// Making sure we have the fruit available for juice (^__^)
global.setTimeout(function () {
global.location.href += "!";
}, 50);
};
global.onhashchange = function () {
if (global.location.hash !== _hash) {
global.location.hash = _hash;
}
};
global.onload = function () {
noBackPlease();
// Disables backspace on page except on input fields and textarea..
document.body.onkeydown = function (e) {
var elm = e.target.nodeName.toLowerCase();
if (e.which === 8 && (elm !== 'input' && elm !== 'textarea')) {
e.preventDefault();
}
// Stopping the event bubbling up the DOM tree...
e.stopPropagation();
};
}
})(window);
这是纯JavaScript,所以它可以在大多数浏览器中工作。它也会禁用退格键,但该键将在输入字段和文本区域内正常工作。
建议设置:
将此片段放在单独的脚本中,并将其包含在需要此行为的页面上。在当前设置中,它将执行DOM的onload事件,这是这段代码的理想入口点。
工作演示!
它在以下浏览器中进行了测试和验证,
铬。
Firefox。
ie浏览器(8-11)和Edge。
Safari。
我遇到了这个问题,需要一个在各种浏览器上正确工作的解决方案,包括Mobile Safari(在发布时是iOS 9)。没有一个解决方案是完全正确的。我提供以下建议(在Internet Explorer 11、Firefox、Chrome和Safari上进行了测试):
history.pushState(null, document.title, location.href);
window.addEventListener('popstate', function (event)
{
history.pushState(null, document.title, location.href);
});
注意事项:
history.forward() (my old solution) does not work on Mobile Safari --- it seems to do nothing (i.e., the user can still go back). history.pushState() does work on all of them.
the third argument to history.pushState() is a url. Solutions which pass a string like 'no-back-button' or 'pagename' seem to work OK, until you then try a Refresh/Reload on the page, at which point a "Page not found" error is generated when the browser tries to locate a page with that as its URL. (The browser is also likely to include that string in the address bar when on the page, which is ugly.) location.href should be used for the URL.
the second argument to history.pushState() is a title. Looking around the web most places say it is "not used", and all the solutions here pass null for that. However, in Mobile Safari at least, that puts the page's URL into the history dropdown the user can access. But when it adds an entry for a page visit normally, it puts in its title, which is preferable. So passing document.title for that results in the same behaviour.