Created
August 23, 2016 01:30
-
-
Save zakness/48655d24850ededa9d79ea4f0d55c9d9 to your computer and use it in GitHub Desktop.
Parse a URL string using document.createElement('a')
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
// 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