Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent show when user press back button in browser? #21

Closed
izcream opened this issue Mar 24, 2016 · 12 comments
Closed

prevent show when user press back button in browser? #21

izcream opened this issue Mar 24, 2016 · 12 comments

Comments

@izcream
Copy link

izcream commented Mar 24, 2016

I use this package in my projects and i found some problem, ex: when i created some product success alert will show. it's work correcty but when i go to create product page again and press back to previous page alert will show up again. how to resolve that?

ps.sorry for my bad english

@jolusadev
Copy link

I have the same problem. When I press the back button without edit some item I get the sweetalert.

@uxweb
Copy link
Owner

uxweb commented Apr 2, 2016

@izcream @jolusadev Hi, this is related to how the data is passed to the laravel session driver.

A way to solve this is by forget the message after displaying it:

@if (Session::has('sweet_alert.alert'))
    <script>
        swal({!! Session::get('sweet_alert.alert') !!});
    </script>

    {{ Session::forget('sweet_alert.alert') }}    // This will forget the alert data after displaying it :)
@endif

@jolusadev
Copy link

Thanks for your anwser. That code not work for me. I think is a navigator cache problem with sessions.

@izcream
Copy link
Author

izcream commented May 1, 2016

i tried that code but isn't work for me too.

@design-class
Copy link

Try reload page when alert window closes
For example
swal({...})
.then(function(){
window.location.reload(window.location.href);
});

@uxweb
Copy link
Owner

uxweb commented Jun 22, 2016

This is an issue related on how the browser's cache and cookies work, i think is not an issue with Laravel neither the package.

@uxweb uxweb closed this as completed Jul 10, 2016
@AdamEsterle
Copy link

@uxweb Probably is still here.

Back button just re-runs the JS in cache (so SWAL shows again)

Maybe there should be a default functionality to "show once" somehow - not sure though... perhaps set a variable

var shown = false

if(!shown){
display SWAL
shown = true
}

Not sure if that var will keep scope though after you press the back button...??

@eng-ahmednasser
Copy link

Try this
<script> if (window.performance) { var navEntries = window.performance.getEntriesByType('navigation'); if (navEntries.length > 0 && navEntries[0].type === 'back_forward') { console.log('As per API lv2, this page is load from back/forward'); } else if (window.performance.navigation && window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD) { console.log('As per API lv1, this page is load from back/forward'); } else { console.log('This is normal page load'); @if (Session::has('sweet_alert.alert')) swal( {!! Session::get('sweet_alert.alert') !!} ); {{ Session::forget('sweet_alert.alert') }} // This will forget the alert data after displaying it :) @endif } } else { console.log("Unfortunately, your browser doesn't support this API"); } </script>

@hasanlq69
Copy link

So what the best solution to solve this problem?

@hiski46
Copy link

hiski46 commented May 12, 2023

this is work with blade in laravel 8

@TUNG251294
Copy link

//only add location.reload();
@if(Session::has('success'))
<script>
Swal.fire({
title: "Delete Success!",
text: "{{ Session::get('success') }}",
icon: 'success',
}).then(function() {
location.reload();
location.href = '/users/list'; //if your want to redirect
})
</script>
@endif

@friday-deployer
Copy link

Try this <script> if (window.performance) { var navEntries = window.performance.getEntriesByType('navigation'); if (navEntries.length > 0 && navEntries[0].type === 'back_forward') { console.log('As per API lv2, this page is load from back/forward'); } else if (window.performance.navigation && window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD) { console.log('As per API lv1, this page is load from back/forward'); } else { console.log('This is normal page load'); @if (Session::has('sweet_alert.alert')) swal( {!! Session::get('sweet_alert.alert') !!} ); {{ Session::forget('sweet_alert.alert') }} // This will forget the alert data after displaying it :) @endif } } else { console.log("Unfortunately, your browser doesn't support this API"); } </script>

this worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants