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


当前回答

非常简单和干净的函数打破向后箭头,而不干扰页面之后。

好处:

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>

其他回答

<script>
    window.location.hash = "no-back-button";

    // Again because Google Chrome doesn't insert
    // the first hash into the history
    window.location.hash = "Again-No-back-button"; 

    window.onhashchange = function(){
        window.location.hash = "no-back-button";
    }
</script>

尝试以下方法来防止ie浏览器中的退格键默认为“后退”:

<script language="JavaScript">
    $(document).ready(function() {
    $(document).unbind('keydown').bind('keydown', function (event) {
        var doPrevent = false;

        if (event.keyCode === 8 ) {
            var d = event.srcElement || event.target;
            if ((d.tagName.toUpperCase() === 'INPUT' &&
                 (
                     d.type.toUpperCase() === 'TEXT'     ||
                     d.type.toUpperCase() === 'PASSWORD' ||
                     d.type.toUpperCase() === 'FILE'     ||
                     d.type.toUpperCase() === 'EMAIL'    ||
                     d.type.toUpperCase() === 'SEARCH'   ||
                     d.type.toUpperCase() === 'DATE' )
                ) ||
                d.tagName.toUpperCase() === 'TEXTAREA') {

                     doPrevent = d.readOnly || d.disabled;
                }
                else {
                    doPrevent = true;
                }
            }

            if (doPrevent) {
                event.preventDefault();
            }

            try {
                document.addEventListener('keydown', function (e) {
                    if ((e.keyCode === 13)) {
                        //alert('Enter keydown');
                        e.stopPropagation();
                        e.preventDefault();
                    }
                }, true);
            }
            catch (err) {
            }
        });
    });
</script>
<script src="~/main.js" type="text/javascript"></script>

<script type="text/javascript">
    window.history.forward();

    function noBack() {
        window.history.forward();
    }
</script>

非常简单和干净的函数打破向后箭头,而不干扰页面之后。

好处:

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>

对我来说,这是一个购物订单。所以我禁用了按钮。当用户单击返回时,该按钮仍然被禁用。当他们再次点击返回,然后点击页面按钮继续前进时。我知道他们的订单已经提交,然后跳转到另一页。

在页面实际刷新的情况下,将使按钮(理论上)可用;然后,我能够在页面加载反应,订单已经提交,然后也重定向。