SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Zealot nicc9's Avatar
    Join Date
    Jan 2005
    Location
    New Orleans, LA
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question multiple instances of XMLHttpRequest

    hi all.

    i'm super-new to ajax.

    i got a good book, and there are a lot of examples.

    however, the author don't really explain how to use XMLHttpRequest in a real application, where the user will do several things at the same time.

    my question is: for each request, you have to create a separate XMLHttpRequest, right?

    because on the book they showed this code:

    Code:
    var xmlHttp;
    
    function createXMLHttpRequest() {
    
      if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
      }
    
      else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
      }
    
    } // function createXMLHttpRequest
    and this is fine if on the page there is only one request, as they create a global var XMLHttpRequest.

    However, I thought that it could happen that there are many requests at once (like if the user, while it's waiting for the first request to be completed, continues doing stuff, starting another request).

    So, I changed it to this:

    Code:
    function createXMLHttpRequest() {
    
    var xmlHttp;
    
      if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
      }
    
      else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
      }
    
    return xmlHttp;
    
    } // function createXMLHttpRequest
    and for each call I do:

    Code:
    var xmlHttp1 = createXMLHttpRequest();
    var xmlHttp2 = createXMLHttpRequest();
    // etc...
    is this the right way to go?

    thanks!

  2. #2
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey nicc9,

    Yes it does work fine.
    check this code.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT=" VIkrnat Korde">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    <script>
    function createXMLHttpRequest() {
    
    var xmlHttp;
    
      if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
      }
    
      else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
      }
    
    return xmlHttp;
    
    } // function createXMLHttpRequest
    
    var xmlhttp1 = createXMLHttpRequest();
    var xmlhttp2 = createXMLHttpRequest();
    
    xmlhttp1.open("GET", "http://www.google.com",true);
    xmlhttp1.onreadystatechange=function() {
      if (xmlhttp1.readyState==4) {
       alert("hi 1");
      }
     }
     xmlhttp1.send(null);
    xmlhttp2.open("GET", "http://www.yahoo.com",true);
    xmlhttp2.onreadystatechange=function() {
      if (xmlhttp2.readyState==4) {
       alert("hi 2");
      }
     }
     xmlhttp2.send(null);
    </script>
    </HEAD>
    
    <BODY>
    
    </BODY>
    </HTML>
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  3. #3
    SitePoint Zealot nicc9's Avatar
    Join Date
    Jan 2005
    Location
    New Orleans, LA
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, ok because I also read somewhere that one is supposed (not sure how) to keep one single xmlHttp object and make all the calls using that same one...

    so creating many instances is the way to go?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •