2014-12-08 56 views
0

我有一個eventlistener需要運行,一旦運行,我需要重定向到另一個頁面。然而,當我在代碼中的任何地方添加一個window.location.href時,它會直接跳轉到重定向,而不會執行其他代碼。eventlistener後重定向到另一個頁面

當我註釋掉運行時,它重定向...

  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

我相信,在這種情況下,你需要在運行重定向之前成功的方法添加到您的最後一個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'; 
    } 
}); 
+0

太棒了,謝謝你! – 2014-12-08 09:17:47

相關問題