get subdomain of url

Hi,

I wish to grab the subdomain portion of a url using javascript. I have this client's url:
http://medicaid-florida.medicaidconsultants.com/

I want to grab only the portion 'medicaid-florida' and save it to a variable.

Is there an easy way to do this?

Thank you,
vkimura
Victor KimuraSEO, Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Samuel LiewCommented:

var subdomain=window.location.href.split("/")[2].split(".")[0];
alert(subdomain);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Samuel LiewCommented:
The code below is if you already have the URL stored in a variable
var domain = "http://medicaid-florida.medicaidconsultants.com/";
var subdomain = domain.split("/")[2].split(".")[0];
alert(subdomain);

Open in new window

0
govindarajan78Commented:
another method:
var subdomain=document.location.href.replace("http://","").replace(".medicaidconsultants.com/","")

but if you have page name and querystrings then then this code will fail.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

halfbloodprinceCommented:
THIS WORKS!!!!:
function getSubDomain()
        {
            var str = "http://medicaid-florida.medicaidconsultants.com/"
            var arrValue = str.split("//");
            var arrSecondValue = arrValue[1].split(".");

            alert(arrSecondValue[0]);

        }
0
halfbloodprinceCommented:
THIS WORKS!!!!:
function getSubDomain()
        {
            var str = "http://medicaid-florida.medicaidconsultants.com/"
            var arrValue = str.split("//");
            var arrSecondValue = arrValue[1].split(".");
            var result = arrSecondValue[0];
            alert(result);

        }
0
govindarajan78Commented:
instead of
window. location.href
use
window.location.host
0
Victor KimuraSEO, Web DeveloperAuthor Commented:
Hi govindarajan78,

What's the difference between the two?
0
govindarajan78Commented:
the location.host will omit the file names & querystrings if any
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.