简体   繁体   中英

Prevent browser back button to not go to previous page

I want to prevent browser back button to not go to previous page.
For this I tried below code:

<script type = "text/javascript" >
        function preventBack() { window.history.forward(); }
        setTimeout("preventBack()", 0);
        window.onunload = function () { null };
    </script>

but when I click on browser back button continuously 2-3 times at one go, it again redirects to previous page. So my above code fails.
I don't want user to go to previous page.

I tried this code as well:

<script>

    $(document).ready(function () {
        function disableBack() { window.history.forward() }

        window.onload = disableBack();
        window.onpageshow = function (evt) { if (evt.persisted) disableBack() }
    });

</script>

but this code is removing the scroller from browser and I am unable to scroll the page.

Please reply some solution. I tried searching many articles but could not find any solution.

Try this

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

Or you may read this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM