How to extract the hostname portion of a URL in JavaScript

Is there a really easy way to start from a full URL:

document.location.href = "http://aaa.bbb.ccc.com/asdf/asdf/sadf.aspx?blah"

And extract just the host part:

aaa.bbb.ccc.com

There’s gotta be a JavaScript function that does this reliably, but I can’t find it.

15 Answers
15

Leave a Comment