2013-11-28 39 views
0

我有像這樣如何發送鏈接在點擊回調函數

$(function(){ 
    $("selector").on("click", "selector", function(e){ 
     e.preventDefault(); 
     var url = $(this).attr("href"); 

     //run code here then in the callback send the url. 

    }); 
}); 

功能基本上我不希望使用setTimeout的,但我想日誌文件燒製之前完成其日誌window.location.href = url;

這可能來自點擊事件。

+0

當然,這是可能的。只需撥打電話,並在回調中放置location.href呼叫。你不確定什麼? –

回答

1

嘗試使用jQuery done()

$(function(){ 
    $("selector").on("click", "selector", function(e){ 
     e.preventDefault(); 
     var url = $(this).attr("href"); 

      var dfd = $.Deferred(); 

      dfd 
      .done(function(){//log code}) 
      .done(function() { 
      window.location.href=url 
      }); 

    }); 
}); 
+0

var url = $(this).attr(「href」); var dfd = $ .Deferred(); dfd.done(函數(){ \t的console.log(URL); \t DimitriLogEvent(貓,動作); \t警報( 「測試」); }) .done(函數(){ \t alert(「test1」); \t window.location.href = url; }); 這不是什麼東西。我錯過了什麼嗎? –

0

您可以使用jQuery的beforeUnload事件來執行你有什麼特別的技巧它移動到下一個頁面之前。

$('selector').click(function() { 
    $(window).bind('beforeunload', function() { 
     var url = $(this).attr("href"); 
     //run code here then in the callback send the url. 
    }); 
}); 
相關問題