Skip to content

Instantly share code, notes, and snippets.

@zakness
Created August 23, 2016 01:30
Show Gist options
  • Save zakness/48655d24850ededa9d79ea4f0d55c9d9 to your computer and use it in GitHub Desktop.
Save zakness/48655d24850ededa9d79ea4f0d55c9d9 to your computer and use it in GitHub Desktop.
Parse a URL string using document.createElement('a')
// see http://stackoverflow.com/questions/736513
export const stringToLocation = (str = '') => {
const anchor = document.createElement('a')
anchor.href = str
// fix for relative URLS in IE
if (!anchor.host) {
anchor.href = anchor.href
}
// fix pathname in IE
if (!_.startsWith(anchor.pathname, '/')) {
anchor.pathname = '/' + anchor.pathname
}
return _.pick(anchor, 'protocol host hostname port pathname hash search origin'.split(' '))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment