Parsing URLs with Javascript
Oct 30 2014
So this is pretty cool:
var a = document.createElement('a');
a.href = 'http://louis:passwd@lpil.uk:1991/cool-app/?callback=parseRep#hash';
a.protocol; // => 'http:'
a.username; // => 'louis'
a.password; // => 'passwd'
a.hostname; // => 'lpil.uk'
a.port; // => '1991'
a.host; // => 'lpil.uk:1991'
a.pathname; // => '/cool-app/'
a.search; // => '?callback=parseRep'
a.hash; // => '#hash'
The only gotcha seems to be that IE drops the preceding /
from pathname
.