0

Below is the code i have developed till now:

$extension = pathinfo($_SERVER['SERVER_NAME'], PATHINFO_EXTENSION);

if($extension == "de")
{
echo "this is a german site";
}

The above function is simple and works well in php, though i need it in javascript, how should i rewrite it?

6
  • I think this is what you're looking for: stackoverflow.com/questions/8253136/… Also, please give credit to the user who posted the PHP code that you used, see: stackoverflow.com/questions/10604829/… Commented Mar 8, 2014 at 20:53
  • 1
    This isn't a code mill, what have you already tried and what doesn't work? Commented Mar 8, 2014 at 20:53
  • 1
    Do you expect us to rewrite it in javascript? Please try yourself first. If you fail, come back with a specific problem. Commented Mar 8, 2014 at 20:53
  • Did you try to google this: google.com/search?q=javascript+get+domain+extention , basically it is impossible unless you have a list for all TLD's.You can split preg_match etc etc but when have .co.uk you are already screwed. Commented Mar 8, 2014 at 21:03
  • 1
    BTW it is always nice to see someone developed the exact same code someone else made.Déjà vu : stackoverflow.com/a/10604887/1316372 Commented Mar 8, 2014 at 21:07

2 Answers 2

2

Capture the current hostname and break it into an array:

var host = window.location.hostname.split('.');

Then check it:

if(host[host.length-1] === 'de'){
    alert('this is a german site');
}

The last item in the array you capture will be the extension.

An alternative if you don't need to deal with older browsers (IE8 or older) is to use the lastIndexOf function:

if((window.location.hostname.lastIndexOf('.')+1) === 'de'){
    alert('this is a german site');
}

No need to capture in a variable that way, if you only need to call it once.

Sign up to request clarification or add additional context in comments.

7 Comments

@Wind64bit - don't thank me, thank the checkmark. ;)
@Wind64bit if this answer is really what you need, then mark it as a correct answer, it's the green check under the vote arrow. And if you like the answer, then vote it up :P
@HenryW - not needed for the requirements of the question. ask a specific question, get an answer specific to that question. no scope creep!
i agree but if he emigrates, he will go hate you :)
@HenryW - he won't hate me nearly as much as he would if i overengineered a crazy regex solution that he would have to maintain without any tangible benefit.
|
0

This should work:

var domain = (location.host)
var arr=domain.split(".")
extension=arr[arr.length-1]

if(extension=="de")
{
alert("this is a german site");
}

4 Comments

If or switch statement on "uk" tld.
@Ko Cour And can i please ask you one more thing if i was looking for the domain e.x www.cnn.com for the "cnn" i would make that extension=arr[arr.length-2] ?
@KoCour - appreciate you copying my exact answer, albeit in the least efficient way possible. always good to have alternatives.
Im not copying anybody, I started writing my answer when there were no answers.arr.length-2 is good unless you work on co.uk and others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.