Skip to content Skip to sidebar Skip to footer

Rewrite Command From Php To Javascript

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 abov

Solution 1:

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.


Solution 2:

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");
}

Post a Comment for "Rewrite Command From Php To Javascript"