nickcernis
7/27/2016 - 3:34 PM

Parsing URLs in JavaScript

Parsing URLs in JavaScript

// From https://news.ycombinator.com/item?id=12172180
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"