Skip to content

Instantly share code, notes, and snippets.

@passcod
Forked from mrclay/UFCOE.parseUri.js
Created April 24, 2012 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save passcod/2482831 to your computer and use it in GitHub Desktop.
Save passcod/2482831 to your computer and use it in GitHub Desktop.
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