DJ Zen Masta K Posted March 18, 2009 Share Posted March 18, 2009 this is code that was suggest to me, but it doesn't work. i know nothing aobut ajax, so forgive me. i'm trying to auto update just that one div rather than the entire page, as that div gets dynamic content that sometimes times out. <html> <head> <script language="JavaScript" type="text/javascript"> function Ajax ( ) { var xmlhttp=false; try { xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (E) { xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } xmlhttp.open("GET", "songinfo.php", true); xmlhttp.onreadystatechange = function ( ) { if ( ajax.readyState == 4 ) { document.getElementById('songinfo').innerHTML = xmlhttp.responseText; } }; return; } setInterval("Ajax()",10000); </script> </head> <body> <div id="songinfo"> <?php include ("songinfo.php"); ?> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
corbin Posted March 18, 2009 Share Posted March 18, 2009 if ( ajax.readyState == 4 ) { Change ajax to "this" or xmlhttp. ("this" would be better.) Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 so... i'm the noob here, but that just seems weird to me. lol if ( "this".readyState == 4 ) { Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 i'm sure i misunderstood what you were trying to relay, because that most definitely does not work. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 18, 2009 Share Posted March 18, 2009 "this" without the quotes. Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 okay, changing to if ( this.readyState == 4 ) { still not working. Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 also tried if ( xmlhttp.readyState == 4 ) { no dice ??? btw, i appreciate your help. Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 18, 2009 Share Posted March 18, 2009 Try alerting a few things to see if you're variables are what you think they are. // place this line alert(xmlhttp); // before this one xmlhttp.open("GET", "songinfo.php", true); Put a few of those and see what happens. Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 okay, you're onto something. i get an [object] error every 10 seconds. should i replace "songinfo.php" with the full path? EDIT: nevermind, that doesn't work. Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 18, 2009 Share Posted March 18, 2009 Did you add the alert I suggested? What is the text of the error? Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 the text of the error in ie7 is: [object] Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 firefox gives me: [object XMLHttpRequest] feel free to view the entire source code at http://totalxr.com/index3.php just ignore my messy css. Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 18, 2009 Share Posted March 18, 2009 Alrighty, looks like that's not an error, but what would be expected *if* the answer to my first question in the last post -- Did you add the alert I suggested? -- would be "yes". I can't help if you don't follow the debug steps and "echo" that back to me. Try this: if ( xmlhttp.readyState == 4 ) { if (xmlhttp.status!=200) { alert('the request failed'); } document.getElementById('songinfo').innerHTML = xmlhttp.responseText; } else { document.getElementById('songinfo').innerHTML += '<br>ready state:' + xmlhttp.readyState; } I think you'll see what part of your script that should replace. It's likely that you will see a lot of text on your screen. Bear with it, and tell me what happens. Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 apologies for not clearly communicating. yes, i did add the code, that led to me getting the error. i will add the new code you suggested. thank you. Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 18, 2009 Share Posted March 18, 2009 No worries, I see you posted a link to the site while I was posting. You are missing a } at the end of your script. Place that just before the </script> Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 18, 2009 Share Posted March 18, 2009 Another problem you are having is you have multiple HTML and HEAD elements inside the BODY element. the getElementById('songinfo') is probably not able to find your div because of that. Running a document.getElementById('songinfo') in the console confirmed that that function is not finding the element. Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 okay. that is due to using the php include function to include other pages formated in html. i guess i should strip them of the html tags. something i never thought of. i'm learning as i go! good news, i have put in what you have told me to, and i'm not getting an error now. my ajax script is now: <script language="JavaScript" type="text/javascript"> function Ajax () { var xmlhttp=false; try { xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (E) { xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } alert(xmlhttp); xmlhttp.open("GET", "songinfo.php", true); xmlhttp.onreadystatechange = function ( ) { if ( xmlhttp.readyState == 4 ) { if (xmlhttp.status!=200) { alert('the request failed'); } document.getElementById('songinfo').innerHTML = xmlhttp.responseText; } else { document.getElementById('songinfo').innerHTML += '<br>ready state:' + xmlhttp.readyState; } }; return; } setInterval("Ajax()",10000); } </script> i greatyly appreciate you helping me on this. Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 i removed the html, head, and body tags from the includes, still not working. grrrr. maybe i should just stick to html Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 18, 2009 Share Posted March 18, 2009 I cleaned up your js script. It had another error in it. <script> function Ajax () { var xmlhttp=false; try { xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (E) { xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } xmlhttp.open("GET", "songinfo.php", true); xmlhttp.onreadystatechange = function ( ) { if ( Ajax.readyState == 4 ) { if (xmlhttp.status!=200) { alert('the request failed'); } document.getElementById('songinfo').innerHTML = xmlhttp.responseText; } else { document.getElementById('songinfo').innerHTML += '<br>ready state:' + xmlhttp.readyState; } }; setInterval("Ajax()",10000); } </script> Try adding that and see what happens. Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 thank you. replaced my old one with the new one you posted, still not working. Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 18, 2009 Share Posted March 18, 2009 you will need to call the Ajax function to get the ball rolling in the body tag add: <body onload="Ajax()"> Quote Link to comment Share on other sites More sharing options...
DJ Zen Masta K Posted March 18, 2009 Author Share Posted March 18, 2009 i added the onload to the body tag,and now something is happening. in both ie7 and firefox3 when i open the page the memory usage of it shoots way up and keeps going. within less than a minute it was from about 83,000 to almost 300,000. first time it froze up my computer before i realized it was happening, second time i had performance monitor running to check. Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 18, 2009 Share Posted March 18, 2009 My bad, looks like the setInterval needs to be changed to setTimeout You used the setInterval to initiate the whole deal before, but that could result in the request being completed before the DOM is ready. That would mean the response wouldn't have anywhere to be displayed. Hence I suggested using the onload. Once your function is called, it will recursively call itself using setTimeout. setInterval would be multiplying the number of requests, setTimeout is what you want there. 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.