单击浏览器的后退按钮时如何破坏会话

时间:2019-12-31 05:59:18

标签: javascript php laravel session laravel-5

我在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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

     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

        });

      }