SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    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.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    split on the dots and return the one in the middle
    Code:
    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

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI MAte,

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

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •