XMLHttpRequest on page load doesn't work sometimes.

Discussion in 'Javascript' started by mathewda@gmail.com, Jul 21, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. . Guest

    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.


    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, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    0
    Views:
    469
    John Saunders
    Aug 28, 2003
  2. Randy Smith
    Replies:
    2
    Views:
    449
    Randy Smith
    Apr 24, 2007
  3. Stef Mientki
    Replies:
    0
    Views:
    222
    Stef Mientki
    Oct 18, 2008
  4. paul reed

    Custom Control Width sometimes has a value...sometimes doesn't

    paul reed, Feb 17, 2004, in forum: ASP .Net Building Controls
    Replies:
    8
    Views:
    147
    Jeffrey Tan[MSFT]
    Feb 21, 2004
  5. Jesse Schoch
    Replies:
    2
    Views:
    132
    Jesse Schoch
    Oct 2, 2003
Loading...

Share This Page