Home  >  Article  >  Web Front-end  >  How to disable browser back in jquery

How to disable browser back in jquery

藏色散人
藏色散人Original
2020-12-14 10:22:403223browse

Jquery implements the method of disabling the browser's back: first open the corresponding js file; then disable the browser's back and next buttons through the "jQuery(document).ready(function () {...}" method That’s it.

How to disable browser back in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, thinkpad t480 computer.

Recommendation: " jquery video tutorial

jquery disables the browser back button

Use Jquery to disable the browser's back and next buttons:

Sometimes for To prevent users from messing up the access sequence and having to disable the browser's forward and back buttons

jQuery(document).ready(function () {
    if (window.history && window.history.pushState) {
        $(window).on('popstate', function () {
           // 当点击浏览器的 后退和前进按钮 时才会被触发, 
            window.history.pushState('forward', null, ''); 
            window.history.forward(1);
        });
    }
    //在IE中必须得有这两行
    window.history.pushState('forward', null, '');  
    window.history.forward(1);
});

This code mainly uses the window.history object of js;

For example:

If the URL of the current page is: http://localhost:28713/SBNext/index.aspx

Execution: window.history.pushState('forward', null, 'badu.aspx');

Result: Add a record http://localhost:28713/SBNext/index.aspx in the browser history. The url of the current page becomes http://localhost:28713/SBNext/badu.aspx, but The page will not be refreshed, nor will the URL be checked to see if it is correct. At this time, if you click the browser's back button, it will return to the http://localhost:28713/SBNext/index.aspx page, which is still the current page. So this is the above disabled The principle of the back button.

The above is the detailed content of How to disable browser back in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn