2011-03-14 5 views
4

:JavaScript 함수가 정의되지 않았습니까? 파이어 폭스는 JS의이 작품에서 오류를 "정의되지 않은 함수를"던지고 어떤 이유

$(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를 포함 있나요? –

+2

이 자바 스크립트에서 어디에서 던지고 있습니까? 여기에는 많은 코드가 있습니다. 특정 라인에 있습니까? – JasCav

+0

어떤 함수가 정의되지 않았는지 말하지 않습니까? – Znarkus

답변

4

문서 준비 래퍼에 3 개의 함수를 래핑 할 이유가 없습니다. 문서가 준비 될 때까지 의존하지 않는 함수는 호출 될 때까지 실행되지 않습니다. 더 나아가, 문서 준비가 가능하도록 그것들을 래핑함으로써, 당신은 그것들을 그 anon 함수의 범위 내로 강제하게되며, 그것들 외부에서 사용될 수 없습니다.

관련이 없으면 $ .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

자바 스크립트의 범위는 함수 기반입니다.

DOMready에서 실행되고 범위를 벗어나는 함수 안에 3 개의 함수를 정의 했으므로 funcitons도 마찬가지입니다.

다른 말로하면 : 3 가지 기능은 DOmready 기능 내에 만 존재하며 그 기능을 벗어난 곳에서는 사용할 수 없습니다.

+0

Aha. 그래서 그 태그를 제거해야합니까? – iamandrus

+0

아니요, 어쨌든 거기에서 호출되지 않기 때문에 domready 함수 내부에 함수가있는 것은 아닙니다. 당신은 그 호출 밖에서 함수를 넣고 싶을 것이다. – Alex

관련 문제