diff options
author | Brandon Mathis <brandon@imathis.com> | 2011-05-30 00:30:16 -0400 |
---|---|---|
committer | Brandon Mathis <brandon@imathis.com> | 2011-05-30 00:30:16 -0400 |
commit | 8698a276f937cb1cd6f67f7f213e2ea438500d7e (patch) | |
tree | 3963ed8f9750cd565087ce54fe8de38d9f5c606d /source/javascripts/libs/jXHR.js | |
parent | c7d5365f81552cae16bbb91696ca3e67b4a0a2e9 (diff) | |
download | my_new_personal_website-8698a276f937cb1cd6f67f7f213e2ea438500d7e.tar.xz my_new_personal_website-8698a276f937cb1cd6f67f7f213e2ea438500d7e.zip |
Cleaned out public from repository, updated gitignore, added syntax
highlighting tests, improved syntax highlighting and styling of pre
blocks.
Overriding dynamic gist styling.
Added a plugin for pygments caching which should speed things up
terrifically.
added ender.js as a lightweight way of scripting the DOM, events, etc.
Some general typography and semantic html improvements.
Diffstat (limited to '')
-rw-r--r-- | source/javascripts/libs/jXHR.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/source/javascripts/libs/jXHR.js b/source/javascripts/libs/jXHR.js new file mode 100644 index 00000000..6775655c --- /dev/null +++ b/source/javascripts/libs/jXHR.js @@ -0,0 +1,85 @@ +// jXHR.js (JSON-P XHR) +// v0.1 (c) Kyle Simpson +// MIT License + +(function(global){ + var SETTIMEOUT = global.setTimeout, // for better compression + doc = global.document, + callback_counter = 0; + + global.jXHR = function() { + var script_url, + script_loaded, + jsonp_callback, + scriptElem, + publicAPI = null; + + function removeScript() { try { scriptElem.parentNode.removeChild(scriptElem); } catch (err) { } } + + function reset() { + script_loaded = false; + script_url = ""; + removeScript(); + scriptElem = null; + fireReadyStateChange(0); + } + + function ThrowError(msg) { + try { publicAPI.onerror.call(publicAPI,msg,script_url); } catch (err) { throw new Error(msg); } + } + + function handleScriptLoad() { + if ((this.readyState && this.readyState!=="complete" && this.readyState!=="loaded") || script_loaded) { return; } + this.onload = this.onreadystatechange = null; // prevent memory leak + script_loaded = true; + if (publicAPI.readyState !== 4) ThrowError("Script failed to load ["+script_url+"]."); + removeScript(); + } + + function fireReadyStateChange(rs,args) { + args = args || []; + publicAPI.readyState = rs; + if (typeof publicAPI.onreadystatechange === "function") publicAPI.onreadystatechange.apply(publicAPI,args); + } + + publicAPI = { + onerror:null, + onreadystatechange:null, + readyState:0, + open:function(method,url){ + reset(); + internal_callback = "cb"+(callback_counter++); + (function(icb){ + global.jXHR[icb] = function() { + try { fireReadyStateChange.call(publicAPI,4,arguments); } + catch(err) { + publicAPI.readyState = -1; + ThrowError("Script failed to run ["+script_url+"]."); + } + global.jXHR[icb] = null; + }; + })(internal_callback); + script_url = url.replace(/=\?/,"=jXHR."+internal_callback); + fireReadyStateChange(1); + }, + send:function(){ + SETTIMEOUT(function(){ + scriptElem = doc.createElement("script"); + scriptElem.setAttribute("type","text/javascript"); + scriptElem.onload = scriptElem.onreadystatechange = function(){handleScriptLoad.call(scriptElem);}; + scriptElem.setAttribute("src",script_url); + doc.getElementsByTagName("head")[0].appendChild(scriptElem); + },0); + fireReadyStateChange(2); + }, + setRequestHeader:function(){}, // noop + getResponseHeader:function(){return "";}, // basically noop + getAllResponseHeaders:function(){return [];} // ditto + }; + + reset(); + + return publicAPI; + }; +})(window); + |