【问题标题】:How to destroy session when clicks on browser's back button单击浏览器的后退按钮时如何销毁会话
【发布时间】:2020-04-19 18:01:15
【问题描述】:

我正在使用 laravel 中的 session->flash() 方法闪烁成功消息。 但是当用户单击返回按钮时,消息再次出现。如何解决这个问题。 我显示消息的代码是 -

@if(Session::get('success') )
    <script>
        swal({
            text: "{{Session::get('success')}}",
            button: localMsg.ok,

        }).then((isConfirm) => {
        });
    </script>
    @elseif(Session::get('error'))
    <script>
        swal({
            text: "{{Session::get('error')}}",
            button: localMsg.ok,

        }).then((isConfirm) => {
        });

    </script>
@endif

【问题讨论】:

    标签: javascript php laravel session laravel-5


    【解决方案1】:

    您应该销毁会话值以获得成功和错误消息

    @if(Session::get('success') )
        <script>
            swal({
                text: "{{Session::get('success')}}",
                button: localMsg.ok,
    
            }).then((isConfirm) => {
            });
         {{ Session::forget('success'); }} //Add this line to destroy value for 'success'
        </script>
        @elseif(Session::get('error'))
        <script>
            swal({
                text: "{{Session::get('error')}}",
                button: localMsg.ok,
    
            }).then((isConfirm) => {
            });
         {{ Session::forget('error'); }} //Add this line to destroy value for 'error'
        </script>
    @endif
    

    【讨论】:

    • 它的删除会话。甚至不显示会话消息
    【解决方案2】:

    通过这种方式可以获取浏览器的返回按钮事件:

         if (window.history && window.history.pushState) {
    
            window.history.pushState('forward', null, './#forward');
    
            $(window).on('popstate', function() {
              alert('Back button was pressed.'); //here you know that the back button is pressed
            //write code to hide your success message when your clicks on browser back button
    
            });
    
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2015-01-18
      • 2014-08-15
      相关资源
      最近更新 更多