Javascript: Vanilla async get

function xhrSuccess () { this.callback.apply(this, this.arguments); } function xhrError () { console.error(this.statusText); } function loadFile (sURL, fCallback /*, argumentToPass1, argumentToPass2, etc. */) { var oReq = new XMLHttpRequest(); oReq.callback = fCallback; oReq.arguments = Array.prototype.slice.call(arguments, 2); oReq.onload = xhrSuccess; oReq.onerror = xhrError; oReq.open("get", sURL, true); oReq.send(null); } Usage: function showMessage (sMsg) { alert(sMsg + this.responseText); } loadFile("message.txt", showMessage, "New message!\n\n");

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.