Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse a URI using the DOM
parseUri = (uri) ->
result = {}
a = document.createElement 'a'
props = 'protocol hostname host pathname port search hash href'.split ' '
a.href = uri
# Copy relevant properties
result[prop] = a[prop] for prop in props
# For window.location compatibility
result.toString = -> a.href
result.requestUri = a.pathname + a.search
# For JSON
result.toJSON = result.toString
result
var parseUri = function (uri) {
var result = {}, index, prop,
a = document.createElement('a'),
props = 'protocol hostname host pathname port search hash href'.split(' ');
a.href = uri;
// Copy relevant properties
for (index in props) {
prop = props[index];
result[prop] = a[prop];
}
// For window.location compatibility
result.toString = function () {return a.href;}
result.requestUri = a.pathname + a.search;
// For JSON
result.toJSON = result.toString;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment