Xmlhttp Help

liunx

Guest
Has anyone ever used xmlhttp before? I'm trying to make a simple GET request and then refresh the current page. I know thats not really what you're supposed to do, you're normally supposed to grab the returned contents and use it somewhere on the current page. Nevertheless, the code i'm using is as follows.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--> xmlhttp.open("GET", url,  true);<br />  xmlhttp.send(null);<br />  window.location.reload();<!--c2--></div><!--ec2--><br /><br />This code works most of the time, but every so often the request doesn't get made before the page reloads. Any ideas on how I can get something like this working 100% of the time.<br /><br />Here are some of the sites i'm looking at. <br /><a href="http://www.web-design-forum.com/article_554" target="_blank">http://www.web-design-forum.com/article_554</a><br /><a href="http://jibbering.com/2002/4/httprequest.html" target="_blank">http://jibbering.com/2002/4/httprequest.html</a><!--content-->
I've never used it, but I did a little searching on the net and I believe I know what your problem is.<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->xmlhttp.open("GET", url,  true);<!--c2--></div><!--ec2--><br />The third parameter ('true' in this case) specifies whether the request is made synchronously (false) or asynchronously (true). Asynchronous requests are done "in the background" and execution of your script continues while the request is processing. Your window.location.reload(); will execute unless the page request finishes before the script gets to that line.<br /><br />You need to either wait for the asynchronous open command to finish before reloading your page, or you need to execute a synchronous open command, which will halt code execution until the page load completes (but may make the browser appear to be hung, depending on how long the page request takes).<br /><br />Asynchronous open - Using event to trigger window reload when page request is complete:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->xmlhttp.open("GET", url,  true);<br />xmlhttp.onreadystatechange = handleResponse;<br />xmlhttp.send(null);<br /><br />// Script execution continues...<br /><br />function handleResponse() {<br />   if ( xmlhttp.readyState == 4 ) {<br />       window.location.reload();<br />   }<br />}<!--c2--></div><!--ec2--><br /><br />Synchronous open - Script execution stops until page request is complete:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->xmlhttp.open("GET", url,  false);<br />xmlhttp.send(null);<br />window.location.reload();<!--c2--></div><!--ec2--><br />Hope this helps...<!--content-->
Wow, I think it worked...so far so good. For some reason I didn't even think about changing that parameter. Thanks a lot David. If the problem recurs, I'll let you know <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
 
Back
Top