Interpretando URLs com DOM

Interpretando URLs com DOM

· 1 minuto
function parseURL(url) {
  var a = document.createElement('a');
  a.href = url;
  return {
    href: a.href,
    scheme: a.protocol,
    host: a.host,
    port: a.port,
    path: a.pathname,
    query: a.search.charAt(0) == '?' ? a.search.substring(1) : null,
    hash: a.hash.charAt(0) == '#' ? a.hash.substring(1) : null,
  };
}