当前位置:网站首页>Three solutions: when clicking the user to exit the login, press the back button of the browser, and you can still see the previous login page.

Three solutions: when clicking the user to exit the login, press the back button of the browser, and you can still see the previous login page.

2022-07-26 03:32:00 The breeze also misses the rain

         On this question , I've tried many ways . My project is springboot + theamleaf Accomplished , At that time, my shutdown function was through a Tag for backend request , Back end cleanup session, Return to login page . Logically speaking , There's nothing wrong with it , however , By chance , After logging out , Click the browser's back button , You can still go back to the landing page , Because I use Ajax Front end interaction , therefore , The page does not need to be refreshed , You can still request data from the backend . that , Here's the problem , The solution I tried and The last best solution .

1. Disable the browser's back button , The code is as follows :

 <script>
        history.go(1);
        var counter =0;
        if (window.history && window.history.pushState) {
            $(window).on('popstate', function () {
                window.location='login';
                // self.location="orderinfo.html";// If you need to jump to the page, use it 
            });
        }
        window.history.pushState('#',null,'#');// stay IE There must be these two lines in the 
        window.history.forward(1);
 </script>

I tried this method first , I found that I retreated quickly many times , It will fail , And it will affect the address in the address bar . Different browsers may have other problems .

2. It's said that it's because of the cache of the browser , Then disable page caching , The code is as follows :

  <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
  <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
  <META HTTP-EQUIV="Expires" CONTENT="0">

This is a HTML, Need to know , Compatibility processing is troublesome ,chrome Browser not yet supported , And other browsers occasionally fail . And just clearing the cache is useless , It is helpful for some problems , For example, after data execution .

PS: Clear cache in browser , It's used in conjunction with other sentences , You can go back to the page you visited before ,ie The browser must download the latest content from the server , Achieve the effect of refreshing .

3. Since it is the browser cache , Then add the version number , adopt js Add version number to the implementation file , The code is as follows :

 <script type="text/javascript">
        document.write("<script src='**.js?" + Math.random() + "'><\/script>");
 </script>

Since it's the browser cache trick , It's useless not to refresh , Therefore, this method will not work .( Whether it's browser caching or browser backtracking , They are innocent , These are all anti gentlemen , Don't guard against villains .)

4. Final solution ,Ajax Empty to background session, If it works , Then refresh this page , The code is as follows :

    $('#user-exit').click(function () {
        $.ajax({
            url: "/loginOut",
            type: "POST",
            success: function (data) {
                location.reload();
            },
            error: function () {
                alert("Ajax Request error !");
            }
        })
    })

Session It runs on the server side ,JavaScript It runs on the client ,JavaScript You can't run server-side code directly . Not used Ajax, It can be used js, First the Session The value of is passed to an element in the front end , And then use js Access the value of the element .

原网站

版权声明
本文为[The breeze also misses the rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207260319022286.html