How can I stop the browser back button using JavaScript?

前端 未结 30 3304
没有蜡笔的小新
没有蜡笔的小新 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:57
    <script language="JavaScript">
        javascript:window.history.forward(1);
    </script>
    
    0 讨论(0)
  • 2020-11-22 00:58

    This code tested with latest Chrome and Firefox browsers.

        <script type="text/javascript">
            history.pushState(null, null, location.href);
            history.back();
            history.forward();
            window.onpopstate = function () { history.go(1); };
        </script>
    
    0 讨论(0)
  • 2020-11-22 00:58

    I create one HTML page ( index.html ). I also create a one ( mechanism.js ) inside a ( script ) folder / directory. Then, I lay all my content inside of ( index.html ) using form, table, span, and div tags as needed. Now, here's the trick that will make back / forward do nothing!

    First, the fact that you have only one page! Second, the use of JavaScript with span / div tags to hide and display content on the same page when needed via regular links!

    Inside ' index.html ' :

        <td width="89px" align="right" valign="top" style="letter-spacing:1px;">
         <small>
          <b>
           <a href="#" class="traff" onClick="DisplayInTrafficTable();">IN</a>&nbsp;
          </b>
         </small>
         [&nbsp;<span id="inCountSPN">0</span>&nbsp;]
        </td>
    

    Inside ' mechanism.js ' :

        function DisplayInTrafficTable()
        {
         var itmsCNT = 0;
         var dsplyIn = "";
         for ( i=0; i<inTraffic.length; i++ )
         {
          dsplyIn += "<tr><td width='11'></td><td align='right'>" + (++itmsCNT) + "</td><td width='11'></td><td><b>" + inTraffic[i] + "</b></td><td width='11'></td><td>" + entryTimeArray[i] + "</td><td width='11'></td><td>" + entryDateArray[i] + "</td><td width='11'></td></tr>";
         }
         document.getElementById('inOutSPN').innerHTML = "" +
                                                 "<table border='0' style='background:#fff;'><tr><th colspan='21' style='background:#feb;padding:11px;'><h3 style='margin-bottom:-1px;'>INCOMING TRAFFIC REPORT</h3>" + DateStamp() + "&nbsp;&nbsp;-&nbsp;&nbsp;<small><a href='#' style='letter-spacing:1px;' onclick='OpenPrintableIn();'>PRINT</a></small></th></tr><tr style='background:#eee;'><td></td><td><b>###</b></td><td></td><td><b>ID #</b></td><td></td><td width='79'><b>TYPE</b></td><td></td><td><b>FIRST</b></td><td></td><td><b>LAST</b></td><td></td><td><b>PLATE #</b></td><td></td><td><b>COMPANY</b></td><td></td><td><b>TIME</b></td><td></td><td><b>DATE</b></td><td></td><td><b>IN / OUT</b></td><td></td></tr>" + dsplyIn.toUpperCase() + "</table>" +
                                                 "";
         return document.getElementById('inOutSPN').innerHTML;
        }
    

    It looks hairy, but note the function names and calls, embedded HTML, and the span tag id calls. This was to show how you can inject different HTML into same span tag on same page! How can Back/Forward affect this design? It cannot, because you are hiding objects and replacing others all on the same page!

    How to hide and display? Here goes: Inside functions in ' mechanism.js ' as needed, use:

        document.getElementById('textOverPic').style.display = "none"; //hide
        document.getElementById('textOverPic').style.display = "";     //display
    

    Inside ' index.html ' call functions through links:

        <img src="images/someimage.jpg" alt="" />
        <span class="textOverPic" id="textOverPic"></span>
    

    and

        <a href="#" style="color:#119;font-size:11px;text-decoration:none;letter-spacing:1px;" onclick="HiddenTextsManager(1);">Introduction</a>
    

    I hope I did not give you a headache. Sorry if I did :-)

    0 讨论(0)
  • 2020-11-22 00:59

    For Restrict Browser back event

    window.history.pushState(null, "", window.location.href);
    window.onpopstate = function () {
        window.history.pushState(null, "", window.location.href);
    };
    
    0 讨论(0)
  • 2020-11-22 00:59
       <script src="~/main.js" type="text/javascript"></script>
        <script type="text/javascript">
            window.history.forward();
            function noBack() { window.history.forward(); } </script>
    
    0 讨论(0)
  • 2020-11-22 00:59

    ReactJS

    For modal component in ReactJS project, the open or close of the modal, controlling browser back is a necessary action.

    • The stopBrowserBack: the stop of the browser back button functionality, also get a callback function. this callback function is what you want to do:

      const stopBrowserBack = callback => {
        window.history.pushState(null, "", window.location.href);
        window.onpopstate = () => {
          window.history.pushState(null, "", window.location.href);
          callback();
        };
      };
      
    • The startBrowserBack: the revival of the browser back button functionality:

      const startBrowserBack = () => {
        window.onpopstate = undefined;
        window.history.back();
      };
      

    The usage in your project:

    handleOpenModal = () =>
      this.setState(
        { modalOpen: true },
        () => stopBrowserBack(this.handleCloseModal)
      );
    
    handleCloseModal = () =>
      this.setState(
        { modalOpen: false },
        startBrowserBack
      );
    
    0 讨论(0)
提交回复
热议问题