Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect after toastr notification

Tags:

jquery

toastr

I'm trying to redirect after toastr notification finishes displaying. I currently have ajax request as

 $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('value')
            },
            type: $(form).attr('method'),
            url: $(form).attr('action'),
            data: $(form).serialize(),
            dataType: 'json',
            success: function (data) {
                toastr.success('Hello','Your fun',{timeOut: 2000,preventDuplicates: true, positionClass:'toast-top-center'});


                     return window.location.href = '/';

            },
            error: function (data) {
                    var html = '<div class="alert alert-danger">Email/Password is invalid</div>';
                    $('#loginMsg').html(html);
            }

The problem is it shows the notification but redirects to quickly to actual read the notification. How can I redirect only after toastr notification hides?

like image 521
Eric Evans Avatar asked Dec 19 '22 13:12

Eric Evans


2 Answers

toastr gives the callback options

toastr.options.onShown = function() { console.log('hello'); } toastr.options.onHidden = function() { console.log('goodbye'); } toastr.options.onclick = function() { console.log('clicked'); } toastr.options.onCloseClick = function() { console.log('close button clicked'); }

inside the function you can use redirect URL

it depends on the plugin which your using check here

like image 160
nmanikiran Avatar answered Apr 02 '23 01:04

nmanikiran


I hope this helps😊

$.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('value')
        },
        type: $(form).attr('method'),
        url: $(form).attr('action'),
        data: $(form).serialize(),
        dataType: 'json',
        success: function(data) {
            toastr.success('Hello', 'Your fun', {
                timeOut: 2000,
                preventDuplicates: true,
                positionClass: 'toast-top-center',
                // Redirect 
                onHidden: function() {
                    window.location.href = '/';
                }
            });
        },
        error: function(data) {
            var html = '<div class="alert alert-danger">Email/Password is invalid</div>';
            $('#loginMsg').html(html);
        }
    });
like image 26
Ferdy Avatar answered Apr 02 '23 00:04

Ferdy