AJAX the XMLHttpRequest Onreadystatechange Event

download AJAX the XMLHttpRequest Onreadystatechange Event

of 2

Transcript of AJAX the XMLHttpRequest Onreadystatechange Event

  • 8/12/2019 AJAX the XMLHttpRequest Onreadystatechange Event

    1/2

    8/8/13 AJAX The XMLHttpRequest onreadystatechange Event

    w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp 1/2

    HOME HTML CSS JAVASCRIPT JQUERY XML ASP.NET PHP SQL MORE... REFERENCES | EXAMPLES | FORUM | ABOU

    Get Certified

    Study Web Technologiesand get a diplomaat w3schools.com

    SHARE THIS PAGE

    Like 77k

    AJAXBasic

    AJAX HOME

    AJAX Intro

    AJAX Example

    AJAXXMLHttpRequest

    XHR Create Object

    XHR Request

    XHR Response

    XHR readyState

    AJAXAdvanced

    AJAX ASP/PHP

    AJAX Database

    AJAX XML File

    AJAXExamples

    AJAX Examples

    Previous Next Chapter

    Previous Next Chapter

    AJAX - The onreadystatechangeEvent

    The onreadystatechange event

    When a request to a server is sent, we want to perform some actions based on the response.

    The onreadystatechange event is triggered every time the readyState changes.

    The readyState property holds the s tatus of the XMLHttpRequest.

    Three important prope rties of the XMLHttpRequest ob ject:

    Property Description

    onreadystate change Stores a function (or the name of a function) to be called automatically each time the

    readyState property changes

    readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:0: request no t initialized

    1: server connection es tablished

    2: request received

    3: processing request

    4: request finished and response is ready

    status 200: "OK"

    404: Page not found

    In the onreadystatechange event, we specify what will happen when the server response is ready to be

    processed.

    When readyState is 4 and s tatus is 200, the response is ready:

    Example

    xmlhttp.onreadystatechange=function()

    {

    if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }

    }

    Try it yourself

    Note:The onreadysta techange event is triggered five times (0-4), one time for each change in readyState.

    Using a Callback Function

    A callback function is a function pass ed as a parameter to ano ther function.

    If you have more than one AJAX task on your webs ite, you should create ONE standard function for creating the

    XMLHttpRequest object, and call this for each AJAX task.

    The function call should contain the URL and what to do on onreadystatechange (which is probably different foreach call):

    Example

    function myFunction()

    {

    loadXMLDoc("ajax_info.txt",function()

    {

    if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }

    });

    }

    Try it yourself

    WEB HOSTING

    Best Web Hosting

    eUK Web Hosting

    UK Reseller Hosting

    Domain, Hosting & Emai

    WEB BUILDING

    XML Editor - Free Trial!

    FREE Website BUILDER

    FREE Website C reator

    Best Website Templates

    STATISTICS

    Browser Statistics

    OS Statistics

    Display Statistics

    Search w3schools.com:

    Select Language

    http://w3schools.com/ajax/ajax_database.asphttp://w3schools.com/browsers/browsers_stats.asphttp://www.altova.com/ref/?s=w3s_text2&q=xmlspyhttp://www.googleadservices.com/pagead/aclk?sa=L&ai=CAJVrKOIDUtPZBOO5igfrqoC4D-6Kg9oF3tSVrnj5_d54EAEg5-j6AVC3wLCN-f____8BYOXC5IOkDqABivP0yQPIAQLgAgCoAwHIA50EqgS0AU_QUcLnTjKiElL6io87gk8CKjSFq4kNMoBw3DWOGxgYr2o_ywR7dCTX2_V9zz4nI8zwzhyF2UQI-2TTivWc8qPoMvaoiWBkzBE2VGNUz-to2L1GL-8SsG5D174cOA8k698a9Kp6KljjkGAw2Z2OK-8oqNJsDVpEsqiHTdX0NKISQm671R7iWp8BcJfRs9IgMocDf3FfzRvUswY1CczzQy5qNqzxpR4HpbsrYec4NGE_MkLGReAEAYgGAaAGAoAH3oyLNg&num=1&cid=5Gj8Oi6wC0c0FkapHuljbsKc&sig=AOD64_1EmTFSsVsRFOtgcd31SYjOSEvkZQ&client=ca-pub-3440800076797949&adurl=http://neo4j.org/learnhttp://www.googleadservices.com/pagead/aclk?sa=L&ai=CAJVrKOIDUtPZBOO5igfrqoC4D-6Kg9oF3tSVrnj5_d54EAEg5-j6AVC3wLCN-f____8BYOXC5IOkDqABivP0yQPIAQLgAgCoAwHIA50EqgS0AU_QUcLnTjKiElL6io87gk8CKjSFq4kNMoBw3DWOGxgYr2o_ywR7dCTX2_V9zz4nI8zwzhyF2UQI-2TTivWc8qPoMvaoiWBkzBE2VGNUz-to2L1GL-8SsG5D174cOA8k698a9Kp6KljjkGAw2Z2OK-8oqNJsDVpEsqiHTdX0NKISQm671R7iWp8BcJfRs9IgMocDf3FfzRvUswY1CczzQy5qNqzxpR4HpbsrYec4NGE_MkLGReAEAYgGAaAGAoAH3oyLNg&num=1&cid=5Gj8Oi6wC0c0FkapHuljbsKc&sig=AOD64_1EmTFSsVsRFOtgcd31SYjOSEvkZQ&client=ca-pub-3440800076797949&adurl=http://neo4j.org/learnhttp://www.googleadservices.com/pagead/aclk?sa=L&ai=CAJVrKOIDUtPZBOO5igfrqoC4D-6Kg9oF3tSVrnj5_d54EAEg5-j6AVC3wLCN-f____8BYOXC5IOkDqABivP0yQPIAQLgAgCoAwHIA50EqgS0AU_QUcLnTjKiElL6io87gk8CKjSFq4kNMoBw3DWOGxgYr2o_ywR7dCTX2_V9zz4nI8zwzhyF2UQI-2TTivWc8qPoMvaoiWBkzBE2VGNUz-to2L1GL-8SsG5D174cOA8k698a9Kp6KljjkGAw2Z2OK-8oqNJsDVpEsqiHTdX0NKISQm671R7iWp8BcJfRs9IgMocDf3FfzRvUswY1CczzQy5qNqzxpR4HpbsrYec4NGE_MkLGReAEAYgGAaAGAoAH3oyLNg&num=1&cid=5Gj8Oi6wC0c0FkapHuljbsKc&sig=AOD64_1EmTFSsVsRFOtgcd31SYjOSEvkZQ&client=ca-pub-3440800076797949&adurl=http://neo4j.org/learnhttp://void%280%29/http://w3schools.com/browsers/browsers_display.asphttp://w3schools.com/browsers/browsers_os.asphttp://w3schools.com/browsers/browsers_stats.asphttp://www.dreamtemplate.com/?ref=w3txtfreehttp://www.wix.com/eteamhtml/400?utm_campaign=ma_w3schools.com&experiment_id=ma_w3schools.comlink2_400http://www.wix.com/eteamhtml/400?utm_campaign=ma_w3schools.com&experiment_id=ma_w3schools.comlink1_400Html&embed_tags=camp&utm_camp=hOUdCO-AmAMQm__j8AMhttp://www.altova.com/ref/?s=w3s_text2&q=xmlspyhttp://www.webhosting.uk.com/cpanel-hosting.phphttp://www.heartinternet.co.uk/http://www.eukhost.com/http://www.lunarpages.com/id/w3schools/goto/w3schoolshttp://w3schools.com/ajax/tryit.asp?filename=tryajax_callbackhttp://w3schools.com/ajax/tryit.asp?filename=tryajax_firsthttp://w3schools.com/ajax/ajax_aspphp.asphttp://w3schools.com/ajax/ajax_xmlhttprequest_response.asphttp://w3schools.com/ajax/ajax_aspphp.asphttp://w3schools.com/ajax/ajax_xmlhttprequest_response.asphttp://w3schools.com/ajax/ajax_examples.asphttp://w3schools.com/ajax/ajax_xmlfile.asphttp://w3schools.com/ajax/ajax_database.asphttp://w3schools.com/ajax/ajax_aspphp.asphttp://w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asphttp://w3schools.com/ajax/ajax_xmlhttprequest_response.asphttp://w3schools.com/ajax/ajax_xmlhttprequest_send.asphttp://w3schools.com/ajax/ajax_xmlhttprequest_create.asphttp://w3schools.com/ajax/ajax_example.asphttp://w3schools.com/ajax/ajax_intro.asphttp://w3schools.com/ajax/default.asphttp://www.stumbleupon.com/submit?url=http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp%26title%3DAJAX%20The%20XMLHttpRequest%20onreadystatechange%20Eventhttp://digg.com/submit?url=http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp&title=AJAX%20The%20XMLHttpRequest%20onreadystatechange%20Eventhttp://www.reddit.com/submit?url=http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asphttp://delicious.com/save?v=5&noui&jump=close&url=http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp&title=AJAX%20The%20XMLHttpRequest%20onreadystatechange%20Eventmailto:?&subject=AJAX%20The%20XMLHttpRequest%20onreadystatechange%20Event&body=Take%20a%20look%20at%20this%20page%20at%20W3Schools.com:%20http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asphttp://twitter.com/home?status=Currently%20reading%20http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asphttp://www.facebook.com/sharer.php?u=http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asphttp://adclick.g.doubleclick.net/aclk?sa=L&ai=BNS3vKOIDUoObBKy7igeqg4DACI_fz4gDAAAAEAEg5-j6ATgAWJf9-Y1IYOXC5IOkDrIBDXczc2Nob29scy5jb226AQlnZnBfaW1hZ2XIAQnaAURodHRwOi8vdzNzY2hvb2xzLmNvbS9hamF4L2FqYXhfeG1saHR0cHJlcXVlc3Rfb25yZWFkeXN0YXRlY2hhbmdlLmFzcKkCbwbuXP6-hj7AAgLgAgDqAhovMTY4MzMxNzUvVG9wTGVmdFJlY3RhbmdsZfgCgdIekAOaCJgDyAaoAwHQBJBO4AQBoAYW&num=0&sig=AOD64_3bSNAdQN_mvaHruoVsOYwV3TUR_Q&client=ca-pub-3440800076797949&adurl=http://w3schools.com/cert/default.asphttp://w3schools.com/about/default.asphttp://w3schools.com/forum/default.asphttp://w3schools.com/sitemap/default.asp#exampleshttp://w3schools.com/sitemap/default.asp#referenceshttp://w3schools.com/sitemap/default.asphttp://w3schools.com/sql/default.asphttp://w3schools.com/php/default.asphttp://w3schools.com/aspnet/default.asphttp://w3schools.com/xml/default.asphttp://w3schools.com/jquery/default.asphttp://w3schools.com/js/default.asphttp://w3schools.com/css/default.asphttp://w3schools.com/html/default.asphttp://w3schools.com/default.asphttp://www.w3schools.com/
  • 8/12/2019 AJAX the XMLHttpRequest Onreadystatechange Event

    2/2

    8/8/13 AJAX The XMLHttpRequest onreadystatechange Event

    w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp 2/2

    REPORT ERROR | HOME | TO P | PRINT | FORUM | ABOUT | ADVERTISE WITH US

    Top 10 Tutorials

    HTML Tutorial

    HTML5 Tutorial

    CSS Tutorial

    CSS3 Tutorial

    JavaScript Tutorial

    jQuery Tutorial

    SQL Tutorial

    PHP Tutorial

    ASP.NET Tutorial

    XML Tutorial

    Top 10 References

    HTML/HTML5 Reference

    CSS 1,2,3 Reference

    CSS 3 Browser Support

    JavaScript

    HTML DOM

    XML DOM

    PHP Reference

    jQuery Reference

    ASP.NET Reference

    HTML Colors

    Examples

    HTML Examples

    CSS Examples

    XML Examples

    JavaScript Examples

    HTML DOM Examples

    XML DOM Examples

    AJAX Examples

    ASP.NET Examples

    Razor Examples

    ASP Examples

    SVG Examples

    Quizzes

    HTML Quiz

    HTML5 Quiz

    XHTML Quiz

    CSS Quiz

    JavaScript Quiz

    jQuery Quiz

    XML Quiz

    ASP Quiz

    PHP Quiz

    SQL Quiz

    Color Picker

    Statistics

    Browser Statistics

    Browser OS

    Browser Display

    W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.

    While using this site, you agree to have read and accepted our terms of use and privacy policy.Copyright 1999-2013 by Refsnes Data. All Rights Reserved.

    http://www.googleadservices.com/pagead/aclk?sa=L&ai=CFPOaKOIDUrPXBOLPigfK1IAwkLrkgwT4gvTKJfmo__cyEAEg5-j6AVDlitPL-v____8BYOXC5IOkDqABtN3q7wPIAQKpAlw9ZHPmrlE-4AIAqAMByAOdBKoEvwFP0EsY1f3Uo0k3KjcbvaS5m8VM0UKt9TVY4Iw7pS5q5IEdZLQ7icz6t6iVVmbG6h2rTh_FTxvPM7dM0leAlhi9w3rHI2yOA8f6uJEmTNXbrybspsuVxwm6gSMkV8jLDZUsi87Dd4RaZ7nm_vHwDNFtAtEf4JxL0i8jS3g42Mvp7joPiAe44E7UshFm4MN-NNoWaBohbmQX8abcbYjRADKw4DcQmoKdNIa57AD2eov6BgIfv2d4qrYoyW4TTlyaA-AEAYgGAaAGAoAHtKKVEA&num=1&cid=5GhLOQWTA5JWpqYKxxjUwuFv&sig=AOD64_1lw6HeVoODvhZy0Tqn31k7EvOPPw&client=ca-pub-3440800076797949&adurl=http://profile.HindiMatrimony.com/campaign/matrimony.php%3Fcampaignid%3DCI-NewsTheme-BR%26adgroupid%3DLive-Hindi-News%26googleaccountid%3D974-249-9880%26searchcontent%3Dcontent%26themeid%3D14%26variantid%3D4%26CON%3DRI%26NET%3DCI%26PUB%3Dgoogle%26MODE%3DCPC%26creative%3D9993527480%26match%3DBroad%26A%3DBharat%26B%3Dw3schools.com%26keyword%3Dhttp://w3schools.com/about/about_copyright.asphttp://w3schools.com/about/about_privacy.asphttp://w3schools.com/about/about_copyright.asphttp://w3schools.com/browsers/browsers_display.asphttp://w3schools.com/browsers/browsers_os.asphttp://w3schools.com/browsers/browsers_stats.asphttp://w3schools.com/tags/ref_colorpicker.asphttp://w3schools.com/quiztest/quiztest.asp?qtest=SQLhttp://w3schools.com/quiztest/quiztest.asp?qtest=PHPhttp://w3schools.com/quiztest/quiztest.asp?qtest=ASPhttp://w3schools.com/quiztest/quiztest.asp?qtest=XMLhttp://w3schools.com/quiztest/quiztest.asp?qtest=jQueryhttp://w3schools.com/quiztest/quiztest.asp?qtest=JavaScripthttp://w3schools.com/quiztest/quiztest.asp?qtest=CSShttp://w3schools.com/quiztest/quiztest.asp?qtest=XHTMLhttp://w3schools.com/quiztest/quiztest.asp?qtest=HTML5http://w3schools.com/quiztest/quiztest.asp?qtest=HTMLhttp://w3schools.com/svg/svg_examples.asphttp://w3schools.com/asp/asp_examples.asphttp://w3schools.com/aspnet/webpages_examples.asphttp://w3schools.com/aspnet/aspnet_examples.asphttp://w3schools.com/ajax/ajax_examples.asphttp://w3schools.com/dom/dom_examples.asphttp://w3schools.com/htmldom/dom_examples.asphttp://w3schools.com/js/js_examples.asphttp://w3schools.com/xml/xml_examples.asphttp://w3schools.com/css/css_examples.asphttp://w3schools.com/html/html_examples.asphttp://w3schools.com/tags/ref_colornames.asphttp://w3schools.com/aspnet/webpages_ref_classes.asphttp://w3schools.com/jquery/jquery_ref_selectors.asphttp://w3schools.com/php/php_ref_array.asphttp://w3schools.com/dom/dom_nodetype.asphttp://w3schools.com/jsref/default.asphttp://w3schools.com/jsref/default.asphttp://w3schools.com/cssref/css3_browsersupport.asphttp://w3schools.com/cssref/default.asphttp://w3schools.com/tags/default.asphttp://w3schools.com/xml/default.asphttp://w3schools.com/aspnet/default.asphttp://w3schools.com/php/default.asphttp://w3schools.com/sql/default.asphttp://w3schools.com/jquery/default.asphttp://w3schools.com/js/default.asphttp://w3schools.com/css3/default.asphttp://w3schools.com/css/default.asphttp://w3schools.com/html/html5_intro.asphttp://w3schools.com/html/default.asphttp://w3schools.com/about/about_advert.asphttp://w3schools.com/about/default.asphttp://w3schools.com/forum/default.asphttp://w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp?output=printhttp://w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp#tophttp://w3schools.com/default.asphttp://w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asphttp://www.w3schools.com/