1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

AJAX check for changes

Discussion in 'JavaScript' started by mattgoody, Feb 22, 2008.

  1. #1
    I currently have a page with a setInterval() timer set to refresh a div every 60s. I'm wondering how i can check to see if there are any changes, and only if there are, then use ajax to reload the div. Also, is there any way to compare the current contents of the div to what the new contents would be and only update the changes? Here is my current js code:
    function ajaxReload(){
    	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){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    		}
    	}
    	
    	ajaxRequest.open("GET", "outlive2.php" , true);
    	ajaxRequest.send(null); 
    }
    setInterval("ajaxReload()", 60000);
    
    Code (markup):
    Thanks
    -Matt
     
    mattgoody, Feb 22, 2008 IP