2011-03-14 77 views
4

出於某種原因,Firefox是扔在這片JS的錯誤「沒有定義功能」:JavaScript函數未定義?

$(function() { // on document ready 

function updateAlerts() { 
    $.ajax({ 
     url : "/check.php", 
     type : "POST", 
     data : { 
     method : 'checkAlerts' 
     }, 
     success : function(data, textStatus, XMLHttpRequest) { 
     var response = $.parseJSON(data); 

     // Update the DOM to show the new alerts! 
     if (response.friendRequests > 0) { 
      // update the number in the DOM and make sure it is visible... 
      $('#notifications').show().text(response.friendRequests); 
     } 
     else { 
      // Hide the number, since there are no pending friend requests or messages 
      var ablanknum = '0'; 
      $('#notifications').show().text(ablanknum); 
     } 

     } 
    }); 
} 

function friendRequestAlert() { 
    $.ajax({ 
     url : "/check.php", 
     type : "POST", 
     data : { 
     method : 'sendFriendAlert' 
     }, 
     success : function(data, textStatus, XMLHttpRequest) { 
     var response = $.parseJSON(data); 

     if (response.theFRAlert !== '0') { 
      // Display our fancy Javascript notification. 
      $.jgrowl('' + response.theFRAlert + ''); 
     } 

     } 
    }); 
} 

function messageAlert() { 
    $.ajax({ 
     url : "/check.php", 
     type : "POST", 
     data : { 
     method : 'sendMessageAlert' 
     }, 
     success : function(data, textStatus, XMLHttpRequest) { 
     var response = $.parseJSON(data); 

     if (response.theAlert !== '0') { 
      // Display our fancy Javascript notification. 
      $.jgrowl('' + response.theAlert + ''); 
      $('#therearemessages').show().text(response.theAlert); 
     } 

     } 
    }); 
} 

}); 

我通過我的代碼並沒有什麼檢查,似乎是錯誤的。

+1

您有加入jQuery的? – 2011-03-14 15:01:25

+2

它在哪裏扔它在JavaScript的這塊?這裏有很多代碼行。它是否在特定的路線? – JasCav 2011-03-14 15:01:53

+0

它沒有說什麼函數沒有定義? – Znarkus 2011-03-14 15:02:19

回答

4

沒有理由將3個函數包裝在文檔就緒包裝中 - 這些函數中的任何內容(可能依賴於準備好的文檔)都會被執行直到它們被調用。此外,通過將它們包裝在文檔中,你迫使它們進入該匿名函數的範圍,並且不能從其外部使用它們。

無關,您應該在$ .ajax調用中將dataType設置爲'json',並停止對$ .parseJSON進行手動調用。

新代碼:

function updateAlerts() 
{ 
    $.ajax({ 
     url: '/check.php', 
     type: 'POST', 
     data: { 
      method: 'checkAlerts' 
     }, 
     dataType: 'json', 
     success: function(response) 
     { 
      // Update the DOM to show the new alerts! 
      if(response.friendRequests > 0) 
      { 
       // update the number in the DOM and make sure it is visible... 
       $('#notifications').show().text(response.friendRequests); 
      } 
      else 
      { 
       // Hide the number, since there are no pending friend requests or messages 
       var ablanknum = '0'; 
       $('#notifications').show().text(ablanknum); 
      } 
     } 
    }); 
} 

function friendRequestAlert() 
{ 
    $.ajax({ 
     url: '/check.php', 
     type: 'POST', 
     data: { 
      method: 'sendFriendAlert' 
     }, 
     dataType: 'json', 
     success: function(response) 
     { 
      if(response.theFRAlert !== '0') 
      { 
       // Display our fancy Javascript notification. 
       $.jgrowl('' + response.theFRAlert + ''); 
      } 
     } 
    }); 
} 

function messageAlert() 
{ 
    $.ajax({ 
     url: '/check.php', 
     type : 'POST', 
     data: { 
      method : 'sendMessageAlert' 
     }, 
     dataType: 'json', 
     success: function(response) 
     { 
      if(response.theAlert !== '0') 
      { 
       // Display our fancy Javascript notification. 
       $.jgrowl('' + response.theAlert + ''); 
       $('#therearemessages').show().text(response.theAlert); 
      } 
     } 
    }); 
} 
4

javascript中的作用域是基於函數的。

由於您在DOMready上運行的函數內定義了3個函數,然後超出了範圍,所以這些函數也是如此。

換句話說:3個函數只存在於DOmready函數中,並且您不能在該函數之外的任何地方使用它們。

+0

啊哈。所以我應該刪除這些標籤? – iamandrus 2011-03-14 15:08:24

+0

不,但是在domready函數中看到沒有意義的功能,因爲它們不會在那裏被調用。你想把這個函數放在這個調用之外,並且調用這些函數 – Alex 2011-03-14 15:11:26