2012-04-08 137 views
0

我遇到了这个聊天系统的一些问题。它不断给我回这些错误,它不应该。Javascript弹出错误

它不断给我弹出说:“地鼠”和

我看过了代码,但不能发现任何错误这很烦人。任何人都知道什么错

<script type="text/javascript"> 

    var maxUsers = 50; // maximum is 50; 
    var updateResponse = ""; 
    var stampResponse = ""; 
    var resetResponse = ""; 
    var stampRef = ""; 
    var pollInterval = ""; 
    var nUsers = []; 
    var nUserColor = ["#ff00ff","#ff6347","#1e90ff","#9932cc","#daa520","#a52a2a","#C0C0C0","#FFFF99","#CC6633","#FF9900"]; 

    function parseUpdateResponse(){ 

     var msgHistory = updateResponse; 
     var tmpUsers = msgHistory.match(/\~([^\~]+)\@/g); 
     if (tmpUsers != null) 
      { 
      tmpUsers = tmpUsers.join("").replace(/[\~\s]/g,"").split("@"); 
      tmpUsers.length = tmpUsers.length-1; 
      nUsers = []; 
      var obj = new Object(); 
      for (i=0; idx=tmpUsers[i]; i++) 
       { 
       obj[idx] = 1; 
       } 
      for (idx in obj) 
       { 
       nUsers.push(idx); 
       } 
      } 
     msgHistory = msgHistory.replace(/\#/g,"<br>").split("|"); 
     for (i=0; i<nUsers.length; i++) 
      { 
      for (n=0; n<msgHistory.length; n++) 
       { 
       if(msgHistory[n].match(nUsers[i]) != null) 
        { 
        msgHistory[n] = msgHistory[n].replace(/\~([^\~]+)\~/,"<strong><font color="+nUserColor[i]+">$1: <\/font><\/strong>"); 
        } 
       } 
      } 
     msgHistory = msgHistory.join(""); 
     var nChatBox = document.getElementById('msgDisp'); 
     nChatBox.innerHTML = msgHistory; 
     if (document.forms[0]['user'].value == "") 
      { 
      document.forms[0]['user'].focus(); 
      } 
     else { 
      document.forms[0]['message'].focus(); 
      } 
     if (nChatBox.scrollHeight > 0) 
      { 
      nChatBox.scrollTop = nChatBox.scrollHeight; 
      } 
    } 

    function updateChat(){ 

     var updateRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); 
     updateRequest.onreadystatechange = function() 
      { 
      if (updateRequest.readyState == 4) 
       { 
       if (updateRequest.status == 200) 
        { 
        updateResponse = updateRequest.responseText; 
        parseUpdateResponse(); 
        } 
       else { 
        alert('Error updateChat.php File '+ updateRequest.statusText); 
        } 
       } 
      } 
     var forceGET = "?n="+ parseInt(Math.random()*999999999); 
     updateRequest.open("GET", "ChatFiles/updateChat.php"+forceGET, true); 
     updateRequest.send(null); 
     pollInterval = setInterval("pollStamp()",7500); 
    } 

    function parseStampResponse(){ 

     var currStamp = stampResponse; 
     if (currStamp != stampRef) 
      { 
      stampRef = currStamp; 
      clearInterval(pollInterval); 
      updateChat(); 
      } 
    } 

    function pollStamp(){ 

     var stampRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); 
     stampRequest.onreadystatechange = function() 
      { 
      if (stampRequest.readyState == 4) 
       { 
       if (stampRequest.status == 200) 
        { 
        stampResponse = stampRequest.responseText; 
        parseStampResponse(); 
        } 
       else { 
        alert('gopher '+ stampRequest.statusText); 
        } 
       } 
      } 
     var forceGET = "?n="+ parseInt(Math.random()*999999999); 
     stampRequest.open("GET", "ChatFiles/pollStamp.php"+forceGET, true); 
     stampRequest.send(null); 
    } 

    function sendMessage(){ 

     var sendRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); 
     sendRequest.onreadystatechange = function() 
      { 
      if (sendRequest.readyState == 4) 
       { 
       if (sendRequest.status == 200) 
        { 
        pollStamp(); 
        } 
       else { 
        alert('Error newMessage.php File '+ sendRequest.statusText); 
        } 
       } 
      } 
     var forceGET = "?n="+ parseInt(Math.random()*999999999); 
     var infoStr = forceGET +"&user="+document.forms[0]['user'].value; 
     infoStr += "&message="+document.forms[0]['message'].value; 
     document.forms[0]['message'].value = ""; 
     sendRequest.open("GET", "ChatFiles/newMessage.php"+infoStr, true); 
     sendRequest.send(null); 
    } 

    function checkSubmit(evt){ 

     var msgBox = document.forms[0]['message']; 
     msgBox.value = msgBox.value.replace(/[\r\n\~|#@]/g,""); 
     var key = (window.Event) ? evt.which : evt.keyCode; 
     if (key == 13) 
      { 
      if (msgBox.value == "" || document.forms[0]['user'].value == "") 
       { 
       return false; 
       } 
      if (nUsers.length == 0) 
       { 
       sendMessage(); 
       return false; 
       } 
      else { 
       for (i=0; i<nUsers.length; i++) 
        { 
        if (document.forms[0]['user'].value == nUsers[i]) 
         { 
         sendMessage(); 
         return false; 
         } 
        } 
       } 
      if (nUsers.length < maxUsers) 
       { 
       sendMessage(); 
       return false; 
       } 
      else { 
       alert('No more than '+maxUsers + ' Users at a time'); 
       } 
      } 
    } 

    function parseResetResponse(){ 

     document.getElementById('pWord').value = resetResponse; 
     if (resetResponse == "Okay") 
      { 
      nUsers.length = 0; 
      document.forms[0]['user'].value = ""; 
      document.forms[0]['message'].value = ""; 
      } 
    } 

    function resetChat(){ 

     var resetRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); 
     resetRequest.onreadystatechange = function() 
      { 
      if (resetRequest.readyState == 4) 
       { 
       if (resetRequest.status == 200) 
        { 
        resetResponse = resetRequest.responseText; 
        parseResetResponse(); 
        } 
       else { 
        alert('Error resetChat.php File '+ resetRequest.statusText); 
        } 
       } 
      } 
     var forceGET = "?n="+ parseInt(Math.random()*999999999); 
     var infoStr = forceGET + "&admin="+document.getElementById('pWord').value; 
     resetRequest.open("GET", "ChatFiles/resetChat.php"+infoStr, true); 
     resetRequest.send(null); 
    } 

    onload=updateChat; 

</script> 
</head> 
    <body> 
     <div id='msgDisp' class='chatBox'></div> 
     <br> 
     <form action=""> 
     <hr size=1> 
     <center>Press enter on your keyboard to submit your message</center> 
     <hr size=1> 
     <input type="hidden" name="user" value="<? echo("$_SESSION[usr_name]");?>" />  
     <br /> 
     <textarea name='message' rows='4' cols='92' style='overflow:auto' onkeyup="checkSubmit(event)"></textarea> 
      </fieldset> 
     </form> 
     <br> 
    </body> 
</html> 

由于提前,

+0

这是很好,你张贴你的代码,但请尝试缩小它一点点,你认为造成的问题。例如。 *什么时候发生?点击某些后?找到与之相对应的代码。此外,如果它说'gopher',请尝试在代码中搜索该单词。 – Armatus 2012-04-08 09:25:23

+0

它不会一直发生,它主要发生在大约5分钟左右之后。我尝试了使用Google搜索并查看问题所在,甚至试图将文件移动到相同的文件夹中,但未修复它。感谢无论如何:) – 2012-04-08 09:27:28

+0

omg使用jquery ... – dynamic 2012-04-08 09:53:32

回答

0

尝试删除行105:

     alert('gopher '+ stampRequest.statusText); 

希望这有助于!

0

您的功能pollStampChatFiles/pollStamp.php发送AJAX请求。这将返回一个错误(可能是404,页面没有找到),这将导致该位:

stampRequest.onreadystatechange = function() 
{ 
    if (stampRequest.readyState == 4) 
    { 
     if (stampRequest.status == 200) 
     { 
      stampResponse = stampRequest.responseText; 
      parseStampResponse(); 
     } 
     else { 
      alert('gopher '+ stampRequest.statusText); 
     } 
    } 
} 

进入了“其他”分支和警觉“地鼠”。 pollStamp设置为在页面加载后的功能updateChat加载页面后执行7.5秒。删除'alert'行(第105行)停止消息,但它不能解决对“pollStamp.php”脚本的请求不起作用。

检查pollStamp.php是否位于给定路径中:它需要位于文件夹ChatFiles中,该文件夹需要与脚本本身位于同一文件夹中。如果没有,请在此处调整路径或将文件移到其所属的位置。