const parseUrl = url => {
const a = document.createElement('a');
a.href = url;
return {
hash: a.hash,
host: a.host,
hostname: a.hostname,
href: a.href,
origin: a.origin,
pathname: a.pathname,
port: a.port,
protocol: a.protocol,
search: a.search
};
};
parseUrl('http://what.com:3000/foo/?bar&baz#header');
Outputs:
{
"hash": "#header",
"host": "what.com:3000",
"hostname": "what.com",
"href": "http://what.com:3000/foo/?bar&baz#header",
"origin": "http://what.com:3000",
"pathname": "/foo/",
"port": "3000",
"protocol": "http:",
"search": "?bar&baz"
}