-
-
Save CitizenOfRome/3719936 to your computer and use it in GitHub Desktop.
Parse a URI using the browser's DOM
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
(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