dzhax Posted March 14, 2011 Share Posted March 14, 2011 so i have 2 sets of functions doing ajax calls.my first one works fine: function sendChat(newChat){ processAjax('../tvdemo/lib/chat/checkChat.php?action=sendChat&message='+newChat+'&user='+getCookie('username')); scroll2Bottom(); document.chatSubmit.reset(); } function processAjax(url) { if (window.XMLHttpRequest) { // Non-IE browsers req = new XMLHttpRequest(); req.onreadystatechange = targetDiv; try { req.open("GET", url, true); } catch (e) { alert(e); } req.send(null); } else if (window.ActiveXObject) { // IE req = new ActiveXObject("Microsoft.XMLHTTP"); if (req) { req.onreadystatechange = targetDiv; req.open("GET", url, true); req.send(); } } } function targetDiv() { if (req.readyState == 4) { // Complete if (req.status == 200) { // OK response document.getElementById("chattingWindow").innerHTML = document.getElementById("chattingWindow").innerHTML; } else { alert(e); } } } <form name="chatSubmit" id="chatSubmit" method="post" style="margin:0;"> <input id="addText" name="addText" style="width:210px" onkeydown="if (event.keyCode == 13) document.chatSubmit.submitChat.click();"> <input name="submitChat" type="button" value="Send" style="width:50px;" onclick="sendChat(addText.value)"></form> However my second one doesnt: function processAjaxChat(url) { if (window.XMLHttpRequest) { // Non-IE browsers req = new XMLHttpRequest(); req.onreadystatechange = targetDivChat; try { req.open("GET", url, true); } catch (e) { alert(e); } req.send(null); } else if (window.ActiveXObject) { // IE req = new ActiveXObject("Microsoft.XMLHTTP"); if (req) { req.onreadystatechange = targetDivChat; req.open("GET", url, true); req.send(); } } } function targetDivChat() { if (req.readyState == 4) { // Complete if (req.status == 200) { // OK response document.getElementById('chattingWindow').innerHTML = req.responseText; scroll2Bottom(); } else { alert(e); } } } <body onload="processAjaxChat('../tvdemo/lib/chat/chechChat.php?action=getChat')"><div id="chattingWindow" style="width: 278px; height:418px; overflow-x:hidden; overflow-y:auto; border:0px transparent solid;"></div> As you can see its a chat program and basically I can post with no problem. but when i run the second set of functions to show the chats on the page, nothing shows up at all...if it helps the page in question is http://www.standthemup.org/tv/index2.htmlthe chat window is supposed to show to the right of the the ustream feed and above the textbox and send button. Link to comment Share on other sites More sharing options...
thescientist Posted March 14, 2011 Share Posted March 14, 2011 are you checking your error console? In Chrome I got two errors just from loading the page, one being a 404 error (page not found) and it's throwing an undefined error when you alert 'e'. e exists like that only within the context of a try/catch block. Link to comment Share on other sites More sharing options...
dzhax Posted March 14, 2011 Author Share Posted March 14, 2011 are you checking your error console? In Chrome I got two errors just from loading the page, one being a 404 error (page not found) and it's throwing an undefined error when you alert 'e'. e exists like that only within the context of a try/catch block.was using firefox. ie dosent show up properly, where do you see the errors in chrome? I just installed it. Link to comment Share on other sites More sharing options...
thescientist Posted March 14, 2011 Share Posted March 14, 2011 when I land on the page. do you have the error console open? Link to comment Share on other sites More sharing options...
dzhax Posted March 14, 2011 Author Share Posted March 14, 2011 like i said i just downloaded it, ive actually never used chrome before...I made a new function set from that code because i need to load some more stuff function processAjaxVariable(window,url) { if (window.XMLHttpRequest) { // Non-IE browsers req = new XMLHttpRequest(); req.onreadystatechange = targetDivVariable(window); try { req.open("GET", url, true); } catch (e) { alert(e); } req.send(null); } else if (window.ActiveXObject) { // IE req = new ActiveXObject("Microsoft.XMLHTTP"); if (req) { req.onreadystatechange = targetDivVariable(window); req.open("GET", url, true); req.send(); } }}function targetDivVariable(divId) { if (req.readyState == 4) { // Complete if (req.status == 200) { // OK response document.getElementById(divId).innerHTML = req.responseText; } else { alert(e); } }} if i were to call the following shouldnt this work??? processAjaxVariable("chatWindow","lib/chechChat.php?action=getChat"); Link to comment Share on other sites More sharing options...
jeffman Posted March 14, 2011 Share Posted March 14, 2011 1. That code is terribly inefficient. Handling the IE6 problem can be much simpler: req = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;req.onreadystatechange = some_function;AJAX.open("GET", url, true);AJAX.send(); 2. function processAjaxVariable(window,url) Using window as a variable identifier is potentially dangerous. If it does not refer to an actual window object, then things like window.XMLHttpRequest will be undefined.Also, this line does not do what you want: req.onreadystatechange = targetDivVariable(window); Instead, it executes targetDivVariable immediately. This might be closer to what you need: req.onreadystatechange = function () { targetDivVariable(window);} Link to comment Share on other sites More sharing options...
dzhax Posted March 15, 2011 Author Share Posted March 15, 2011 thanks that cleared it up. it appears to work now Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.