Results 1 to 3 of 3

Thread: Deeper look to Ajax with javaScript script

  1. #1
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Exclamation Deeper look to Ajax with javaScript script

    Hi,

    I saw a script in w3schools. And Below I have written some comments near functions, but still - there I wrote some questions and you will see them in the script...

    Please answer most understandly thanks.

    Here is the script:
    PHP Code:
    var xmlhttp;

    //function ShowUser which generates link (with information) to send to php script. 

    function showUser(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (
    xmlhttp==null)
      {
      
    alert ("Browser does not support HTTP Request");
      return;
      }
    var 
    url="getuser.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }


    //Function StateChanged, is active only when you choose other value in the form, or I AM WRONG????

    function stateChanged()
    {
    if (
    xmlhttp.readyState==4)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }

    //And this function is always needed. Maybe to send and get back information?? What does this function? Do I have to use it fully, or I can ommit something?

    function GetXmlHttpObject()
    {
    if (
    window.XMLHttpRequest)
      {
      
    // code for IE7+, Firefox, Chrome, Opera, Safari
      
    return new XMLHttpRequest();
      }
    if (
    window.ActiveXObject)
      {
      
    // code for IE6, IE5
      
    return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return 
    null;


    The real script is HERE

    Comment this topic as best you can, because I take a deep interest in every word you will say. Really serious in understanding this. THANKS.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    //function ShowUser which generates link (with information) to send to php script.
    Not a link, a URL to be requested from the server. The "information" (an accurate but generic term), is in this case a query string that the server and (presumably) the PHP script treat as GET data.

    //Function StateChanged, is active only when you choose other value in the form, or I AM WRONG????
    Wrong. The showUser function is only active when the form's select's onchange event fires. StateChanged is only active if the user's browser supports one of the two methods for making a request that are in the GetXmlHttpObject function and that request's readystate changes.

    //And this function [sic: GetXmlHttpObject] is always needed. Maybe to send and get back information?? What does this function? Do I have to use it fully, or I can ommit something?
    It's used by showUser to determine which of two methods for performing the request a browser may support. Preference is given to the XMLHttpRequest() method, in that if a browser supports both methods, XMLHttpRequest is the one that will be used. There are at least two others that are omitted here, but only one of those may be important, that for the icebrowser. Omitting anything material from GetXmlHttpObject will only limit the number of browsers supported. GetXmlHttpObject could be rewritten to be more efficient. But it probably would then be even less clear what it's doing.

    The request object generated by GetXmlHttpObject is modified by the showUser function and then used by showUser to send information to the server requesting a page (in this case a PHP script, that presumably will return the desired information as the request object's responseText, an HTML string). If the request object's readystate changes (which it will do several times during the normal execution of an eventually successful request), showUser has configured it (the request object) to fire stateChanged, which in turn tests the readystate to see if it is 4 (complete). If so the HTML string returned as the responseText is placed in the txtHint element.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    That was really helpful, thanks.

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
  •