Skip to main content

get root URL in javascript



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.



Source: Tips4all

Comments

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

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

    ReplyDelete
  2. 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'

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

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?