在eventlistener之后重定向到另一个页面

时间:2014-12-08 00:15:35

标签: javascript

我有一个需要运行的eventlistener,一旦运行我需要重定向到另一个页面。但是当我在我的代码中的任何地方添加一个window.location.href时,它只是直接跳转到重定向而不会执行其他代码。

当我注释掉重定向时,它会运行它......

            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

1 个答案:

答案 0 :(得分:1)

我相信在这种情况下,您需要在运行重定向之前为上一次ajax调用添加成功方法。

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