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

XMLHttpRequest on page load doesn't work sometimes.

P: n/a
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();

Jul 21 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
.
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.
ma******@gmail.com ha scritto:
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();
Jul 21 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.