Last active
March 7, 2016 23:18
-
-
Save devfred/2726307 to your computer and use it in GitHub Desktop.
javascript: Xhr with queue example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Queue to use in conjuction with xhr functionality */ | |
var requestQ = (function() { | |
var numRequestToComplete, requestsCompleted, callBacks, singleCallBack; | |
return function(options) { | |
if (!options) options = {}; | |
numRequestToComplete = options.numRequest || 0; | |
requestsCompleted = options.requestsCompleted || 0; | |
callBacks = []; | |
var fireCallbacks = function() { | |
/* alert("we're all complete"); */ | |
for (var i = 0; i < callBacks.length; i++) callBacks[i](); | |
}; | |
if (options.singleCallback) callBacks.push(options.singleCallback); | |
this.addCallbackToQueue = function(isComplete, callback) { | |
if (isComplete) requestsCompleted++; | |
if (callback) callBacks.push(callback); | |
if (requestsCompleted == numRequestToComplete) fireCallbacks(); | |
}; | |
this.requestComplete = function(isComplete) { | |
if (isComplete) requestsCompleted++; | |
if (requestsCompleted == numRequestToComplete) fireCallbacks(); | |
}; | |
this.setCallback = function(callback) { | |
callBacks.push(callBack); | |
}; | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* XHR */ | |
(function(win, doc){ | |
//http://code.google.com/p/browsersec/wiki/Part2#Same-origin_policy_for_XMLHttpRequest | |
"use strict"; | |
var xhr = { | |
_XMLHttpFactories: [ | |
function () {return new XMLHttpRequest()}, | |
function () {return new ActiveXObject("Msxml2.XMLHTTP")}, | |
function () {return new ActiveXObject("Msxml3.XMLHTTP")}, | |
function () {return new ActiveXObject("Microsoft.XMLHTTP")} | |
], | |
_createXMLHTTPObject: function(){ | |
var xmlhttp = false; | |
for (var i=0;i< xhr._XMLHttpFactories.length;i++) { | |
try { xmlhttp = xhr._XMLHttpFactories[i](); } | |
catch (e) { continue;} | |
break; | |
} | |
return xmlhttp; | |
}, | |
sendRequest: function(url, cb, data) { | |
var req = xhr._createXMLHTTPObject(); | |
if (!req) return; | |
var method = (data) ? "POST" : "GET"; | |
req.open(method, url, true); | |
//req.setRequestHeader('User-Agent', 'XMLHTTP/1.0'); | |
if (data) req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | |
req.onreadystatechange = function() { | |
if (req.readyState != 4) return; | |
if (req.status != 200 && req.status != 304) { | |
//alert('HTTP error ' + req.status); | |
return; | |
} | |
cb(req); | |
} | |
if (req.readyState == 4) return; | |
req.send(data); | |
} | |
}; | |
// example | |
xhr.sendRequest(window.location.href, function( req ){ | |
console.log(req); | |
}, 'a=2&b=3&c=4'); | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment