mozilla

Compare Revisions

Synchronous and asynchronous requests

Change Revisions

Revision 415223:

Revision 415223 by fusionchess on

Revision 429903:

Revision 429903 by fusionchess on

Title:
Synchronous and asynchronous requests
Synchronous and asynchronous requests
Slug:
Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests
Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests
Tags:
"Gecko DOM Reference", "DOM", "XMLHttpRequest"
"Gecko DOM Reference", "DOM", "XMLHttpRequest"
Content:

Revision 415223
Revision 429903
nn58function xhrSuccess () { this.callback.apply(this, this.arguments
 >); }
59 
60function xhrError () { console.error(this.statusText); }
61 
58function loadFile (urlcallback /*, argumentToPass1, argumentToP62function loadFile (sURLfCallback /*, argumentToPass1, argumentT
>ass2, etc. */) {>oPass2, etc. */) {
59  var args = arguments.slice(2);
60  var xhr = new XMLHttpRequest();63  var oReq = new XMLHttpRequest();
61  xhr.onload = function() {64  oReq.callback = fCallback;
62    if (xhr.readyState === 4) { 65  oReq.arguments = Array.prototype.slice.call(arguments, 2);
63      if (xhr.status === 200) {66  oReq.onload = xhrSuccess;
64        callback.apply(xhr, args);67  oReq.onerror = xhrError;
65      } else {68  oReq.open("get", sURL, true);
66        console.error(xhr.statusText);
67      }
68    }
69  };
70  xhr.open("GET", url, true);
71  xhr.send(null);69  oReq.send(null);
n82loadFile("message.txt", showMessage, "New message!\n");n80loadFile("message.txt", showMessage, "New message!\n\n");
n240    <h4>n238    <h4 id="Example_.231.3A_Automatic_logout_before_exit">
n253    <h4>n251    <h4 id="Example_.232.3A_Noting_if_a_user_abandon_the_site_wit
 >hout_clicking_any_link_and_registering_the_boring_page">
t300    <h2>t298    <h2 id="See_also">

Back to History