单击浏览器的后退按钮时如何销毁会话

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


慕仙森
浏览 126回答 2
2回答

紫衣仙女

您应该销毁会话值以获取成功和错误消息@if(Session::get('success') )&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; swal({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "{{Session::get('success')}}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button: localMsg.ok,&nbsp; &nbsp; &nbsp; &nbsp; }).then((isConfirm) => {&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp;{{ Session::forget('success'); }} //Add this line to destroy value for 'success'&nbsp; &nbsp; </script>&nbsp; &nbsp; @elseif(Session::get('error'))&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; swal({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "{{Session::get('error')}}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button: localMsg.ok,&nbsp; &nbsp; &nbsp; &nbsp; }).then((isConfirm) => {&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp;{{ Session::forget('error'); }} //Add this line to destroy value for 'error'&nbsp; &nbsp; </script>@endif

大话西游666

通过这种方式,您可以获得浏览器的返回按钮事件:&nbsp; &nbsp; &nbsp;if (window.history && window.history.pushState) {&nbsp; &nbsp; &nbsp; &nbsp; window.history.pushState('forward', null, './#forward');&nbsp; &nbsp; &nbsp; &nbsp; $(window).on('popstate', function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('Back button was pressed.'); //here you know that the back button is pressed&nbsp; &nbsp; &nbsp; &nbsp; //write code to hide your success message when your clicks on browser back button&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP