XMLHttpRequest too slow

Discussion in 'Javascript' started by otto, Apr 14, 2005.

  1. otto

    otto Guest

    I have a form validation function that is supposed to use
    XMLHttpRequest to validate a url (i.e. make sure it returns a 200), but
    the problem is sometimes it takes about 5 seconds to return anything
    and sometimes it returns right away with the wrong response.

    What am I doing wrong? Or is it something other than the javascript
    maybe? Thanks.

    function validate(myform){
    //Verify if URLs are valid.
    var page1 = document.getElementById('page1');
    var error = '';
    var xmlhttp = false;
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
    try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(E){
    xmlhttp = false;
    }
    }
    if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    xmlhttp = new XMLHttpRequest();
    }
    xmlhttp.open("HEAD",page1.value,true);
    xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4){
    if(xmlhttp.status != 200){
    error = "Invalid URL";
    }
    }
    }
    xmlhttp.send(null);
    if(error && error != ''){
    alert(error);
    return false;
    }else{
    return true;
    }
    }
    otto, Apr 14, 2005
    #1
    1. Advertising

  2. "otto" <> writes:

    > I have a form validation function that is supposed to use
    > XMLHttpRequest to validate a url (i.e. make sure it returns a 200), but
    > the problem is sometimes it takes about 5 seconds to return anything
    > and sometimes it returns right away with the wrong response.
    >

    ....
    > xmlhttp.open("HEAD",page1.value,true);


    The third argument, true, means that the connection will be
    asynchroneous. That means that calling "send" will return
    immediately, and not wait for the communication to finish.

    > xmlhttp.onreadystatechange = function(){
    > if(xmlhttp.readyState == 4){
    > if(xmlhttp.status != 200){
    > error = "Invalid URL";
    > }
    > }
    > }
    > xmlhttp.send(null);
    > if(error && error != ''){


    Since communication is asynchroneous, it's probably not done
    talking to the server yet when this code is executed. In fact,
    since Javascript is generally not multithreaded, even if it is
    done, the readystatechange handler has not been executed yet.

    I can't see where the 5 second delay comes from in this code.
    It could just be the browser hanging for some reason.

    (btw, "error && error != ''" is superfluous, since '' counts as false.
    Just "error" will do).


    You could do a synchroneous connection instaead (false as third
    argument to "open"). The there won't be any readystatechange events,
    but the call to "send" will only return when the answer is ready.
    ---
    xmlhttp.open("HEAD",page1.value,false);
    xmlhttp.send("");
    error = (xmlthhp.status == 200) ? "" : "Illegal URL";
    ---

    Regards
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 15, 2005
    #2
    1. Advertising

  3. otto wrote:


    > xmlhttp.open("HEAD",page1.value,true);
    > xmlhttp.onreadystatechange = function(){
    > if(xmlhttp.readyState == 4){
    > if(xmlhttp.status != 200){
    > error = "Invalid URL";
    > }
    > }
    > }
    > xmlhttp.send(null);



    > if(error && error != ''){
    > alert(error);
    > return false;
    > }else{
    > return true;
    > }


    It doesn't make sense to have code here after the send() call checking
    error, you should call the send method and any code that has to happen
    once the request has been made and the response has been sent should go
    into the onreadystatechange handler.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 15, 2005
    #3
  4. otto

    otto Guest

    This is great. That's exactly what I needed to know. Thanks to you and
    Martin for the assistance.
    otto, Apr 15, 2005
    #4
    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. Martin
    Replies:
    0
    Views:
    462
    Martin
    Aug 4, 2004
  2. SB
    Replies:
    0
    Views:
    367
  3. Mike Foster
    Replies:
    0
    Views:
    896
    Mike Foster
    Jun 23, 2003
  4. Mark Parnell
    Replies:
    0
    Views:
    811
    Mark Parnell
    Jun 24, 2003
  5. Luigi Donatello Asero

    Are these pictures too dark or/and too large?

    Luigi Donatello Asero, May 19, 2004, in forum: HTML
    Replies:
    13
    Views:
    900
    Mabden
    May 21, 2004
Loading...

Share This Page