Re: XMLHttpRequest on page load doesn't work sometimes.
- From: "." <rdjdvd@xxxxxxxxx>
- Date: 21 Jul 2006 14:40:08 -0700
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.
mathewda@xxxxxxxxx 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();
.
- References:
- XMLHttpRequest on page load doesn't work sometimes.
- From: mathewda
- XMLHttpRequest on page load doesn't work sometimes.
- Prev by Date: JavaScript vs Python
- Next by Date: Re: Script Works in IE not FF--What am I missing?
- Previous by thread: XMLHttpRequest on page load doesn't work sometimes.
- Next by thread: Re: "Automation server can't create Objects" error while accessing the WMI scripts from Linux box
- Index(es):
Relevant Pages
|
Loading