How can I stop the browser back button using JavaScript?

前端 未结 30 3303
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 00:07

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.

相关标签:
30条回答
  • 2020-11-22 00:55

    Try it with ease :

    history.pushState(null, null, document.title);
    window.addEventListener('popstate', function () {
        history.pushState(null, null, document.title);
    });
    
    0 讨论(0)
  • 2020-11-22 00:55

    I had this problem with React (class component).

    And I solved it easily:

    componentDidMount() {
        window.addEventListener("popstate", e => {
            this.props.history.goForward();
        }
    }
    

    I've used HashRouter from react-router-dom.

    0 讨论(0)
  • 2020-11-22 00:56
    <script>
    window.location.hash="no-back-button";
    window.location.hash="Again-No-back-button";//again because google chrome don't insert first hash into history
    window.onhashchange=function(){window.location.hash="no-back-button";}
    </script> 
    
    0 讨论(0)
  • 2020-11-22 00:56

    Just set location.hash="Something". On pressing the back button, the hash will get removed from the URL, but the page won't go back.

    This method is good for preventing going back accidentally, but for security purposes you should design your backend for preventing reanswering.

    0 讨论(0)
  • 2020-11-22 00:56
    //"use strict";
    function stopBackSpace(e) {
        var ev = e || window.event;
        var obj = ev.target || ev.srcElement;
        var t = obj.type || obj.getAttribute('type');
    
        var vReadOnly = obj.getAttribute('readonly');
        var vEnabled = obj.getAttribute('enabled');
        // null
        vReadOnly = (vReadOnly == null) ? false : vReadOnly;
        vEnabled = (vEnabled == null) ? true : vEnabled;
        // when click Backspace,judge the type of obj.
    
        var flag1 = ((t == 'password' || t == 'text' || t == 'textarea') && ((vReadOnly == true || vReadOnly == 'readonly') || vEnabled != true)) ? true : false;
    
        var flag2 = (t != 'password' && t != 'text' && t != 'textarea') ? true : false;
    
        if (flag2) {
            e.keyCode = 0;
            e.cancelBubble = true;
            return false;
        }
        if (flag1) {
            e.keyCode = 0;
            e.cancelBubble = true;
            return false;
        }
    }
    if (typeof($) == 'function') {
        $(function() {
            $(document).keydown(function(e) {
                if (e.keyCode == 8) {
                    return stopBackSpace(e);
                }
            });
        });
    } else {
        document.onkeydown = stopBackSpace;
    }
    
    0 讨论(0)
  • 2020-11-22 00:57

    In my case this was a shopping order. So what I did was disable the button. When the user clicked back, the button was disabled still. When they clicked back one more time, and then clicked a page button to go forward. I knew their order was submitted and skipped to another page.

    In the case when the page actually refreshed which would make the button (theoretically), available; I was then able to react in the page load that the order is already submitted and redirect then too.

    0 讨论(0)
提交回复
热议问题