Knowledge for the World

How to parse a url in JavaScript

Sure there are plugins that will do this for you, but here's a quick and easy way.

1

var parser = document.createElement("a");
parser.href = "http://howchoo.com:8000/interests?query=1#bottom";

2

parser.protocol; // =>"http:" parser.hostname; // => "howchoo.com" parser.port; // =>"8000" parser.pathname; // => "/interests" parser.search; // => "?query=1" parser.hash; // => "#bottom" parser.host; // => "howchoo.com:8000"

3