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?

Wind64bit
  • 7
  • 3
  • 1
    This isn't a code mill, what have you already tried and what doesn't work? – Zarathuztra Mar 08 '14 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. – Marcel Gwerder Mar 08 '14 at 20:53
  • Did you try to google this: https://www.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. – HenryW Mar 08 '14 at 21:03
  • 1
    BTW it is always nice to see someone developed the exact same code someone else made.Déjà vu : http://stackoverflow.com/a/10604887/1316372 – HenryW Mar 08 '14 at 21:07
  • I posted that too Henry – Rstew Mar 08 '14 at 21:15
  • I think this is what you're looking for: http://stackoverflow.com/questions/8253136/how-to-get-domain-name-only-using-javascript Also, please give credit to the user who posted the PHP code that you used, see: http://stackoverflow.com/questions/10604829/check-for-domain-extension-with-php-or-js – Rstew Mar 08 '14 at 20:53

2 Answers2

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.

PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40
  • @Wind64bit - don't thank me, thank the checkmark. ;) – PlantTheIdea Mar 08 '14 at 20:59
  • @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 – Mohammed Joraid Mar 08 '14 at 21:02
  • @HenryW - not needed for the requirements of the question. ask a specific question, get an answer specific to that question. no scope creep! – PlantTheIdea Mar 08 '14 at 21:14
  • i agree but if he emigrates, he will go hate you :) – HenryW Mar 08 '14 at 21:16
  • @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. – PlantTheIdea Mar 08 '14 at 21:21
  • But that would be a hell of an answer – HenryW Mar 08 '14 at 21:24
  • @HenryW - if he hadn't given the checkmark to someone that copied my answer (in a terribly inefficient way no less!) I might be inclined, but at this point not worth it. ;) – PlantTheIdea Mar 08 '14 at 21:26
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");
}
Ko Cour
  • 929
  • 2
  • 18
  • 29
  • If or switch statement on "uk" tld. – Ko Cour Mar 08 '14 at 21:13
  • @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]` ? – Wind64bit Mar 08 '14 at 21:20
  • @KoCour - appreciate you copying my exact answer, albeit in the least efficient way possible. always good to have alternatives. – PlantTheIdea Mar 08 '14 at 21:23
  • 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. – Ko Cour Mar 08 '14 at 21:32