Extracting the domain name from a URL without the extension

I am trying to extract the domain name from a url using javascript but having some problems.

This is what I have:


var publisherName = document.domain;
if(publisherName.indexOf('www')){
  publisherName = document.domain.substr(3,document.domain.length);
}

But it is not working and I am not sure how to rid of the extension…ie…com,co.uk,net etc.

Basically, this is what I want: Whether the location being sub domain or not it need to get from somthing similar to - “http://www.sub.domain.com/dir1/dir2/page.html” to just “domain”.

Any help appreciated.

split on the dots and return the one in the middle


var getpub= function(){
	var d= document.domain.split('.');
	return d[1] || d[0];
}

location.host may be more useful than document.domain-
it will find a file or ftp host, for instance

HI MAte,

I ended up doing this as what you write was a bit unreliable.


function getpub(){
	   var d= document.domain.split('.');
  	   switch(d.length){
          case 2:
            return d[0];
            break;
          
          case 3:
            return d[1];
            break;
       }
}