Comment Comment by cyberpanther Great catch and everyone should know there is an easy way to parse URLs in JS. Just create an anchor element and let the browser parse it for you. Like so:var parser = document.createElement('a');parser.href = "http://example.com:3000/pathname/?search=test#hash";parser.protocol; // => "http:"parser.hostname; // => "example.com"parser.port; // => "3000"parser.pathname; // => "/pathname/"parser.search; // => "?search=test"parser.hash; // => "#hash"parser.host; // => "example.com:3000" parent cyberpanther 10 years ago
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"