By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

Ajax automatic refresh a DIV

P: 38
hi all
i want to refresh a div using ajax,

i wrote this code:

index.php
Expand|Select|Wrap|Line Numbers
  1. <script src="ajax/moniter/moniter.js" type="text/javascript"></script>
  2. <body onload="moniter();">
  3. <div id="moniter">
  4.  
  5.       </div>
  6. </body
  7.  
moniter.js
Expand|Select|Wrap|Line Numbers
  1. // JavaScript Document
  2. var xmlHttp_moniter
  3.  
  4. function moniter()
  5. {
  6.     xmlHttp_moniter = GetXmlHttpObject_parcel()
  7.     if(xmlHttp_moniter == null)
  8.     {
  9.         alert("browser does not support HTTP Request")
  10.         return
  11.     }
  12.     var url="ajax/moniter/moniter.php"
  13.     xmlHttp_moniter.onreadystatechange = stateChanged
  14.     xmlHttp_moniter.open("GET",url,true)
  15.     xmlHttp_moniter.send(null)
  16.  
  17. }
  18.  
  19. function stateChanged()
  20. {
  21.     if(xmlHttp_moniter.readyState==4 || xmlHttp_moniter.readyState == "complete")
  22.     {
  23.         document.getElementById("moniter").innerHTML = xmlHttp_moniter.responseText
  24.         setTimeout('moniter()',100);
  25.     }
  26. }
  27.  
  28. function GetXmlHttpObject_parcel()
  29. {
  30.     var xmlHttp_moniter=null;
  31.     try
  32.     {
  33.         xmlHttp_moniter=new XMLHttpRequest();
  34.     }
  35.     catch (e)
  36.          {
  37.              //Internet Explorer
  38.              try
  39.               {
  40.                   xmlHttp_moniter=new ActiveXObject("Msxml2.XMLHTTP");
  41.               }
  42.              catch (e)
  43.               {
  44.               xmlHttp_moniter=new ActiveXObject("Microsoft.XMLHTTP");
  45.               }
  46.          }
  47.     return xmlHttp_moniter;
  48. }
  49.  
  50.  
  51.  
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
Apr 10 '09 #1
Share this Question
Share on Google+
4 Replies


hsriat
Expert 100+
P: 1,654
Try this:
Expand|Select|Wrap|Line Numbers
  1. 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.
Apr 10 '09 #2

P: 38
hsriat,

You are the man;

thank you,

if anybody wants to use this code, just add hsriat line and it will work perfectilly
Apr 10 '09 #3

acoder
Expert Mod 15k+
P: 16,027
...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.
Apr 10 '09 #4

hsriat
Expert 100+
P: 1,654
@acoder
My mistake... but I wonder how did it work if he used Maths.
Apr 10 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.