-
-
Save passcod/2482831 to your computer and use it in GitHub Desktop.
Parse a URI using the DOM
This file contains hidden or 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
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 |
This file contains hidden or 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
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