Skip to content

Instantly share code, notes, and snippets.

@CitizenOfRome
Forked from mrclay/UFCOE.parseUri.js
Last active April 7, 2017 09:09
Show Gist options
  • Save CitizenOfRome/3719936 to your computer and use it in GitHub Desktop.
Save CitizenOfRome/3719936 to your computer and use it in GitHub Desktop.
Parse a URI using the browser's DOM
(function (w, d) {
//https://gist.github.com/1847816
var a, k = 'protocol hostname host pathname port search hash href'.split(' ');
w.UFCOE = w.UFCOE || {};
/**
* Parse a URI, returning an object similar to Location
*
* Usage: UFCOE.parseUri("hello?search#hash").search -> "?search"
*
* @param String url
* @return Object
*/
w.UFCOE.parseUri = function (url) {
var getAnchor = function(url){
//For IE support : http://stackoverflow.com/a/22918332/937891
var div= d.createElement('div');
div.innerHTML = "<a></a>";
div.firstChild.href = url; // Ensures that the href is properly escaped
div.innerHTML = div.innerHTML; // Run the current innerHTML back through the parser
return div.firstChild.href;
};
a || (a = getAnchor(url)); // Let browser do the work
for (var r = {}, i = 0; i<8; i++) {
r[k[i]] = a[k[i]];
}
r.toString = function() { return a.href; };
r.requestUri = r.pathname + r.search;
return r;
};
})(window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment