Frank Clark
Frank Clark

Reputation: 195

Checking the TLD of a site using Javascript

A quick intro: I am building error pages for a Magento build. I need to identify the top-level-domain for a site so i can do some conditional statements to show different translations for each TLD of the site.

I need some javascript that will just return if the site is .co.uk / .fr / .es / .pt etc.

So that i can hide and show the correct languages are required.

Can anyone give me a hand ?

Thanks in advance.

Upvotes: 0

Views: 836

Answers (2)

Kalpesh
Kalpesh

Reputation: 5685

Don't know why you need JS to do this thing.

Check the below JS code which will detect the domain extension. But if it is .co.uk, it will only say uk, but I think it should be OK to you as you only want to detect the country out of it.

<script type="text/javascript">
var extension=location.hostname.split(".");
extension=extension[extension.length-1];
alert(extension);
</script>

Upvotes: 1

SPRBRN
SPRBRN

Reputation: 2462

You can use the following code in your template:

<?php
$tld = strrchr ( $_SERVER['SERVER_NAME'], "." );
$tld = substr ( $tld, 1 );
?>

Then use this inside the page to set the value in javascript:

<script>
var tld = <?php echo $tld; ?>;
</script>

Upvotes: 0

Related Questions