Jump to content

[SOLVED] Ajax reload only if content has changed?


scuff

Recommended Posts

<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!

 

 

Link to comment
Share on other sites

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;
		}
	}

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.