XMLHttpRequest on page load doesn't work sometimes.

M

mathewda

Hey, I'm having a problem that I consider kinda weird that is alluding
me at the moment. I've wrote some code that will set up an
XMLHttpRequest, it then makes a call to open and send and sets the
onreadystatechange to another function of mine. onreadystatechange
checks the ready state and if the ready state is 4 and the status is
200 it assigns to the innerHTML of a div. I also have some code that
uses the attachEvent method so that all of this is attached to the
onload event. When I test it in IE or Firefox the first time the page
loads it does not work but on a page refresh it works just fine, if I
refresh many times it works most of the time but every so often (maybe
1 out of every 10 or 20 refreshes) it doesn't work. What I find strange
about this is that on the times that it doesn't work the page title
still gets set correctly by a line of code that is in the same block
that sets my div tag's innerHTML so I know that the XMLHttpRequest was
sent and the load was completed... however it seems like the data
wasn't there to assign to my div tag? If anyone could offer up some
suggestions why this works most of the time but not all the time I'd
really appreciate it!

function createAJAXObject()
{
var req = null;
if(window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
req = false;
}
}
}

return req;
}

function AJAX(method, uri, async) {

if (xmlhttp!=null)
{
xmlhttp.open(method, uri, async);
xmlhttp.onreadystatechange = XMLHttpRequestStateChange;
xmlhttp.send(null);
}
else {
//alert("NULL!!!!! :-(");
}

}

function Initialize() {
AJAX("POST", "Bin.aspx", true);
}

function XMLHttpRequestStateChange() {
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
document.getElementById("binDiv").innerHTML =
xmlhttp.responseText;
document.title = "ready state = " + xmlhttp.readyState + "
status = " + xmlhttp.status;
}
else
{
document.getElementById("binDiv").innerHTML = "Error:
XMLHttpRequest.status = " + xmlhttp.status;
}
}
}

function RegisterTestLoadEvents()
{
if (window.addEventListener)
{
// Use W3C DOM event registering method.
window.addEventListener('load', Initialize, false);
}
else
{
// Use Microsoft event registering method.
window.attachEvent('onload', Initialize);
}

}

var xmlhttp = createAJAXObject();
RegisterTestLoadEvents();
 
?

.

Hi Mathewda,
you can see my code (Ajax) in this package, tested on IE, Firefox,
Opera:

locawapp-0e.tar.gz (12 KB)
http://locawapp.atspace.com/

and to write here your comment...
I'm searching programmers for JSython project.


(e-mail address removed) ha scritto:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top