scuff Posted October 6, 2008 Share Posted October 6, 2008 <html> <body onLoad="commencer()"> <script language="javascript" type="text/javascript"> <!-- function commencer() { setInterval(ajaxFunction, 1000); } //Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ document.getElementById('test').innerHTML = ajaxRequest.responseText; } } ajaxRequest.open("POST", "serverTime.php", true); ajaxRequest.send(null); } //--> </script> <div id="test" name="test"> </div> </body> </html> Btw in serverTime.php the code is: <?php echo date("H:i:s"); ?> I'm just wondering if it's possible to only use document.getElementById('test').innerHTML = ajaxRequest.responseText; if the content of ajaxRequest.responseText has changed? I imagine it would be something like this: (this doesn't work) ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ if (document.getElementById('test').innerHTML == ajaxRequest.responseText) { }else{ document.getElementById('test').innerHTML = ajaxRequest.responseText; } } Please help thanks! Quote Link to comment Share on other sites More sharing options...
xtopolis Posted October 6, 2008 Share Posted October 6, 2008 It worked for me. if(ajaxRequest.readyState == 4){ if (document.getElementById('test').innerHTML == ajaxRequest.responseText) { document.getElementById('test').innerHTML = 'was the same'; }else{ document.getElementById('test').innerHTML = ajaxRequest.responseText; } } Quote Link to comment Share on other sites More sharing options...
scuff Posted October 6, 2008 Author Share Posted October 6, 2008 I tested with simpler text in the file and it worked, so I need to find out why it stops sometimes. thanks! (sorry) Quote Link to comment Share on other sites More sharing options...
scuff Posted October 7, 2008 Author Share Posted October 7, 2008 I found out that if you put in the serverTime.php this: <img src="anypic.jpg"> it wouldn't work, not sure if it's because innerHTML doesn't accept it or if I need to do something differently. Please help if you can. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted October 7, 2008 Share Posted October 7, 2008 Show me serverTime.php's code Quote Link to comment Share on other sites More sharing options...
scuff Posted October 7, 2008 Author Share Posted October 7, 2008 serverTime.php's code is <img src='logo.gif'> that's it (I think the <>s are messing it up) Quote Link to comment Share on other sites More sharing options...
xtopolis Posted October 8, 2008 Share Posted October 8, 2008 I assume you're reuploading "logo.gif" assuming that javascript will know that the image itself has changed even though it's the same filename. Javascript doesn't know at all, it's only checking if text is the same. There are a few things you can do..., but it depends on your structure.. -Are you sure this needs to be checked frequently, and reloaded if different? ideas: get a hash value of the image from php using: <?php $hash = md5(file_get_contents('path/to/img.gif')); ?> This will reflect changes in content. Then compare that hash to a hash of the current image they have that you would store somewhere. But I would really work on the idea: "does it really need to be done this way", or maybe explain what you're trying to do so I can better understand and give you other options... Quote Link to comment Share on other sites More sharing options...
scuff Posted October 8, 2008 Author Share Posted October 8, 2008 It's going to reload like a chatboard so it really does need to refresh alot... but even if I do <br> it bugs the script and doesn't sense if it has changed or not (I'm aware it only detects if the text has changed but it doesn't in this case for some reason) it seems the < and >s are bugged in this. Do you know why and could you tell me how to overcome this? BTW this is not neccessary it just seems that it would make it less laggy if it were to only update when the content changes. Thanks Quote Link to comment Share on other sites More sharing options...
xtopolis Posted October 8, 2008 Share Posted October 8, 2008 So, it's a live chat type app? -How is the text stored? Flat file, or database, or other? -I'm not understanding your meaning behind <, > being bugged.. , please site a more specific reference.. -Overcome: Flash app, or AJAX-PHP socket server (both beyond my expertise atm) Depending on your chat text storage method, ie if you're echoing <p>chat text</p>, you could set a fake attribute like <p msg="134">chat text</p>.. and have javascript to check the msg attribute of it's last message vs the msg attribute of the one it's looking at.. if they don't match, show the new content, otherwise, do nothing. This still creates the request to check each time though! 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.