Ajax automatic refresh a DIV

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eihabisaac
    New Member
    • Aug 2007
    • 38

    #1

    Ajax automatic refresh a DIV

    hi all
    i want to refresh a div using ajax,

    i wrote this code:

    index.php
    Code:
    <script src="ajax/moniter/moniter.js" type="text/javascript"></script>
    <body onload="moniter();">
    <div id="moniter">
          
          </div>
    </body
    moniter.js
    Code:
    // JavaScript Document
    var xmlHttp_moniter
    
    function moniter()
    {
    	xmlHttp_moniter = GetXmlHttpObject_parcel()
    	if(xmlHttp_moniter == null)
    	{
    		alert("browser does not support HTTP Request")
    		return
    	}
    	var url="ajax/moniter/moniter.php"
    	xmlHttp_moniter.onreadystatechange = stateChanged
    	xmlHttp_moniter.open("GET",url,true)
    	xmlHttp_moniter.send(null)
    
    }
    
    function stateChanged()
    {
    	if(xmlHttp_moniter.readyState==4 || xmlHttp_moniter.readyState == "complete")
    	{
    		document.getElementById("moniter").innerHTML = xmlHttp_moniter.responseText
    		setTimeout('moniter()',100);
    	}
    }
    
    function GetXmlHttpObject_parcel()
    {
    	var xmlHttp_moniter=null;
    	try
    	{
    		xmlHttp_moniter=new XMLHttpRequest();
    	}
    	catch (e)
     		{
     			//Internet Explorer
     			try
      			{
      				xmlHttp_moniter=new ActiveXObject("Msxml2.XMLHTTP");
      			}
     			catch (e)
      			{
      			xmlHttp_moniter=new ActiveXObject("Microsoft.XMLHTTP");
      			}
     		}
    	return xmlHttp_moniter;
    }
    it work but the results are always the same from moniter.php file

    where is the problem or if anybody know a better way to do ajax div refresh

    thanks in advance
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Try this:
    Code:
    var url="ajax/moniter/moniter.php?random=" + Maths.random()
    This will make sure new request is made every time, instead of picking the content from the local cache.

    Comment

    • eihabisaac
      New Member
      • Aug 2007
      • 38

      #3
      hsriat,

      You are the man;

      thank you,

      if anybody wants to use this code, just add hsriat line and it will work perfectilly

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        ...except that it's Math. To be completely sure that it's unique, use a timestamp: (new Date()).getTime ()

        You could also set headers to make sure the page is not cached.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by acoder
          ...except that it's Math.
          My mistake... but I wonder how did it work if he used Maths.

          Comment

          Working...