prime Posted November 26, 2008 Share Posted November 26, 2008 I have the following code for a chat display app I made and well it works perfectly on some browser but not others, can someone tell me where I went wrong with it please... I think it's something to do with the recursion, but I'm not 100% sure <html><head> <script src="../ajaxrequest.js"></script> <script langauge="JavaScript">function updatePage() { if(request.readyState == 4) { var xmlobj = request.responseXML var id = xmlobj.getElementsByTagName("postid")[0].childNodes[0].nodeValue var user = xmlobj.getElementsByTagName("user")[0].childNodes[0].nodeValue var message = xmlobj.getElementsByTagName("message")[0].childNodes[0].nodeValue usermessage = user + ": " + message; if(previd == undefined) { Display = "<p>Welcome to Primefalcon's AJAX chat have a seat and enjoy your stay</p><br /><br />"; } else if(previd == id) { Display = prevdata; } else { Display = prevdata + "<br />" + usermessage; } document.getElementById("pagedisplay").innerHTML = Display; previd = id; prevdata = Display; }}</script> <style type="text/css"> body { background-color: #000066; } #pagedisplay { color: #000000; } </style> </head><body bgcolor="white"><font color="black"> <script langauge="JavaScript"> var previd; var prevdata; function main() { createRequest(); var serverurl = "server_display.php"; request.open("GET", serverurl, true); request.onreadystatechange = updatePage; request.send(null); //just add + for extra info setTimeout('main()', '300'); } main(); </script> <div id="pagedisplay"></div> </body></html> Quote Link to comment Share on other sites More sharing options...
corbin Posted November 26, 2008 Share Posted November 26, 2008 What browser does it not work in? Quote Link to comment Share on other sites More sharing options...
prime Posted November 26, 2008 Author Share Posted November 26, 2008 it works in my firefox but not in firefox on a different computer, it also has a problem with opera Quote Link to comment Share on other sites More sharing options...
corbin Posted November 26, 2008 Share Posted November 26, 2008 My guess would be that the main function is getting executed more than once at a time. Couple of ways to fix this problem: -Make the setTimeout('main()', '300'); call in the callback function (updatePage). That way, it will be set to run again right after it finishes. -Don't rely on global variables. For example, instead of using request in updatePage (since request is a global variable), use this. this can be used since the method is called form inside of the function. Then, it doesn't matter what the outside variable is. Also, make the createRequest() function return a value, not set a global var. Quote Link to comment Share on other sites More sharing options...
prime Posted November 26, 2008 Author Share Posted November 26, 2008 1 question, the request as in request is my Ajax object which is... var request = null; function createRequest() { try { request = new XMLHttpRequest(); } catch(trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch(othermicrosoft) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(failed) { request = null; } } } } Quote Link to comment Share on other sites More sharing options...
corbin Posted November 27, 2008 Share Posted November 27, 2008 I would be more likely to do it like this: function createRequest() { var request; try { request = new XMLHttpRequest(); } catch(trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch(othermicrosoft){ try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(failed) { request = false; } } } } var request = createRequest(); if(request === false) { alert("Please get a browser with AJAX support."); } If it returns a new instance of an AJAX object, it can be used more easily, and it doesn't rely on a global variable (global variables are evil ;p). If you want, I can tell you the changes I would make to all of your code. Quote Link to comment Share on other sites More sharing options...
prime Posted November 27, 2008 Author Share Posted November 27, 2008 Sure :-) I'm only starting Ajax so I definitely would like to see how someone more experienced would do it better Quote Link to comment Share on other sites More sharing options...
corbin Posted November 28, 2008 Share Posted November 28, 2008 <html> <head> <script src="../ajaxrequest.js"></script> <script language="JavaScript"> function createRequest() { var request; try { request = new XMLHttpRequest(); } catch(trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch(othermicrosoft){ try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(failed) { request = false; } } } return request; } function updatePage() { if(this.readyState == 4) { var xmlobj = this.responseXML; var id = xmlobj.getElementsByTagName("postid")[0].childNodes[0].nodeValue; var user = xmlobj.getElementsByTagName("user")[0].childNodes[0].nodeValue; var message = xmlobj.getElementsByTagName("message")[0].childNodes[0].nodeValue; usermessage = user + ": " + message; if(previd == undefined { Display = "<p>Welcome to Primefalcon's AJAX chat have a seat and enjoy your stay</p><br /><br />"; } else if(previd == id) { Display = prevdata; } else { Display = prevdata + "<br />" + usermessage; } document.getElementById("pagedisplay").innerHTML = Display; previd = id; prevdata = Display; setTimeout('main()', '300'); } } var previd; var prevdata; function main() { var request = createRequest(); var serverurl = "server_display.php"; request.open("GET", serverurl, true); request.onreadystatechange = updatePage; request.send(null); //just add + for extra info } </script> <style type="text/css"> body { background-color: #000066; color: black; } #pagedisplay { color: #000000; } </style> </head> <body bgcolor="white"> <script language="JavaScript"> main(); </script> <div id="pagedisplay"></div> </body> </html> Oh, you misspelled language every time it was in there by the way x.x. Anyway that's more-ish how I would do it. Quote Link to comment Share on other sites More sharing options...
prime Posted December 2, 2008 Author Share Posted December 2, 2008 Thx :-) and dang it your right about the language, thx for pointing that outm that'll help a lot Quote Link to comment Share on other sites More sharing options...
corbin Posted December 2, 2008 Share Posted December 2, 2008 Eh, I wouldn't have caught it if not for syntax highlighting ;p. No problem. Quote Link to comment Share on other sites More sharing options...
prime Posted December 2, 2008 Author Share Posted December 2, 2008 eh I use VI or gedit mostly since I'm using a Linux os, and both of those have syntax highlighting so unfortunately for me, I can't use that Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.