Saher Siddiqui
Saher Siddiqui

Reputation: 363

How to destroy session when clicks on browser's back button

I am flashing a success message using session->flash() method in laravel. But when user clicks back button the message comes up again. How to fix this. My code for showing message is -

@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

Upvotes: 3

Views: 1886

Answers (2)

Harpal Singh
Harpal Singh

Reputation: 702

You should destroy session values for success and error message

@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

Upvotes: 2

shiivamguptaa
shiivamguptaa

Reputation: 84

By This way you can get back button event of the browser:

     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

        });

      }

Upvotes: 1

Related Questions