XMLHttpRequest too slow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • otto

    #1

    XMLHttpRequest too slow

    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.getEle mentById('page1 ');
    var error = '';
    var xmlhttp = false;
    try{
    xmlhttp = new ActiveXObject(" Msxml2.XMLHTTP" );
    }catch(e){
    try{
    xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
    }catch(E){
    xmlhttp = false;
    }
    }
    if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    xmlhttp = new XMLHttpRequest( );
    }
    xmlhttp.open("H EAD",page1.valu e,true);
    xmlhttp.onready statechange = function(){
    if(xmlhttp.read yState == 4){
    if(xmlhttp.stat us != 200){
    error = "Invalid URL";
    }
    }
    }
    xmlhttp.send(nu ll);
    if(error && error != ''){
    alert(error);
    return false;
    }else{
    return true;
    }
    }

  • Lasse Reichstein Nielsen

    #2
    Re: XMLHttpRequest too slow

    "otto" <otto95@pegasi. net> writes:
    [color=blue]
    > 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.
    >[/color]
    ....[color=blue]
    > xmlhttp.open("H EAD",page1.valu e,true);[/color]

    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.
    [color=blue]
    > xmlhttp.onready statechange = function(){
    > if(xmlhttp.read yState == 4){
    > if(xmlhttp.stat us != 200){
    > error = "Invalid URL";
    > }
    > }
    > }
    > xmlhttp.send(nu ll);
    > if(error && error != ''){[/color]

    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 readystatechang e 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 readystatechang e events,
    but the call to "send" will only return when the answer is ready.
    ---
    xmlhttp.open("H EAD",page1.valu e,false);
    xmlhttp.send("" );
    error = (xmlthhp.status == 200) ? "" : "Illegal URL";
    ---

    Regards
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Martin Honnen

      #3
      Re: XMLHttpRequest too slow



      otto wrote:

      [color=blue]
      > xmlhttp.open("H EAD",page1.valu e,true);
      > xmlhttp.onready statechange = function(){
      > if(xmlhttp.read yState == 4){
      > if(xmlhttp.stat us != 200){
      > error = "Invalid URL";
      > }
      > }
      > }
      > xmlhttp.send(nu ll);[/color]

      [color=blue]
      > if(error && error != ''){
      > alert(error);
      > return false;
      > }else{
      > return true;
      > }[/color]

      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 onreadystatecha nge handler.

      --

      Martin Honnen
      http://JavaScript.FAQTs.com/

      Comment

      • otto

        #4
        Re: XMLHttpRequest too slow

        This is great. That's exactly what I needed to know. Thanks to you and
        Martin for the assistance.

        Comment

        Working...