Zoe Epix
Zoe Epix

Reputation: 63

Redirect to another page after eventlistener

I have an eventlistener that needs to run, once its run I need to redirect to another page. However when I add a window.location.href anywhere in my code it just jumps straight to the redirect and doesn't do the other code.

When I comment out the redirect it runs it...

            document.getElementById('save').addEventListener('click', function () {
                /*
                * since the stage toDataURL() method is asynchronous, we need
                * to provide a callback
                */
                stage.toDataURL({
                    callback: function (dataUrl) {
                        //window.open(dataUrl);
                        // Send the screenshot to PHP to save it on the server
                        var url = 'export.php';
                        //alert(url);
                        $.ajax({
                            type: "POST",
                            url: url,
                            dataType: 'text',
                            data: {
                                base64data: dataUrl,
                                yname: thename,
                                yemail: theemail,
                                company: thecomp,
                                twitter: thetwitter
                            }
                        });

                    }
                });
            }); //close listener

Upvotes: 0

Views: 1268

Answers (1)

Kodemon
Kodemon

Reputation: 370

I believe in this case you need to add a success method to your last ajax call before running the redirect.

$.ajax({
    type: "POST",
    url: url,
    dataType: 'text',
    data: {
        base64data: dataUrl,
        yname: thename,
        yemail: theemail,
        company: thecomp,
        twitter: thetwitter
    },
    success: function() {
        location.href = 'your_url';
    }
});

Upvotes: 1

Related Questions