Loading a file on the server - timing issues?

Discussion in 'Javascript' started by joe, Apr 25, 2008.

  1. joe

    joe Guest

    I am loading a text file to a variable with XMLHttpRequest()
    There seems to be some sort of timing issue since loadXML (source code below)
    returns the contents of the file on seconds try. In Firefox I get an empty
    string. No errors are reported.

    If I look at the code with Firefox debugger (Venkman) everything works fine. I
    should probably put a loop somewhere to check when loading the file is finished.


    var xmlhttp;
    ...
    [a lot of Javascipt code here]
    ...
    loadXMLDoc("http://wwww.mysite.com/test.bin");
    mystr=xmlhttp.statusText;
    ...


    function loadXMLDoc(url)
    {
    xmlhttp=null;
    if (window.XMLHttpRequest)
    {// code for IE7, Firefox, Opera, etc.
    xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp!=null)
    {
    xmlhttp.onreadystatechange=state_Change;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    else
    {
    alert("Browser not supported.");
    }
    }

    function state_Change()
    {
    if (xmlhttp.readyState==4)
    {// 4 = "loaded"
    if (xmlhttp.status!=200)
    {
    alert("Problem:" + xmlhttp.statusText);
    }
    }
    }
    joe, Apr 25, 2008
    #1
    1. Advertising

  2. joe wrote:

    > loadXMLDoc("http://wwww.mysite.com/test.bin");
    > mystr=xmlhttp.statusText;


    As your loadXMLDoc does asynchronous loading (third argument to open
    method is true) you can't access the statusText in your code following
    the loadXMLDoc call as that code is executed before the response to the
    HTTP request will have been received.
    Continue to use asynchronous loading but put any code trying to read
    status, statusText and responseXML or responseText into the
    onreadystatechange handler.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 25, 2008
    #2
    1. Advertising

  3. joe

    joe Guest

    Martin Honnen <> wrote:

    >joe wrote:
    >
    >> loadXMLDoc("http://wwww.mysite.com/test.bin");
    >> mystr=xmlhttp.statusText;

    >
    >As your loadXMLDoc does asynchronous loading (third argument to open
    >method is true) you can't access the statusText in your code following
    >the loadXMLDoc call as that code is executed before the response to the
    >HTTP request will have been received.
    >Continue to use asynchronous loading but put any code trying to read
    >status, statusText and responseXML or responseText into the
    >onreadystatechange handler.


    Thanks. It helped.
    joe, Apr 25, 2008
    #3
    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. Brian Hanson

    File.Copy Method timing issues.

    Brian Hanson, Feb 6, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    4,035
  2. Replies:
    1
    Views:
    354
    Brock Allen
    Mar 14, 2005
  3. jaquesgrobler

    Timing issues !!! help help!

    jaquesgrobler, May 8, 2008, in forum: VHDL
    Replies:
    0
    Views:
    507
    jaquesgrobler
    May 8, 2008
  4. Josef Frank
    Replies:
    0
    Views:
    218
    Josef Frank
    Oct 28, 2010
  5. Giles Bowkett

    precise timing issues with ruby

    Giles Bowkett, Jan 9, 2008, in forum: Ruby
    Replies:
    3
    Views:
    103
    Giles Bowkett
    Jan 9, 2008
Loading...

Share This Page