views:

2261

answers:

4

is there a really easy way to turn document.location.href=="http://aaa.bbb.ccc.ddd.com/asdf/sadf/asdf/asdf/asdf/sadf.aspx?blah" into 'aaa.bbb.ccc.ddd.com'?

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

+2  A: 

Try document.location.host or document.location.hostname?

Chris Nielsen
A: 

You can use

window.location.pathname

Oh, no you can't sorry. This returns the last portion of the URL. Oops.

mdresser
+1  A: 

You could concatenate the location protocol and the host:

var root = location.protocol + '//' + location.host;

For a url, let say 'http://stackoverflow.com/questions', it will return 'http://stackoverflow.com'

CMS
+3  A: 

Use document.location object and its host or hostname properties.

alert(document.location.hostname); // alerts "stackoverflow.com"
n1313