Extracting just the country code from URL?

L

Laz

Hi,

I've not worked with javascript, and after searching could not find
example of this type of manipulation.

I'm trying to set a variable to the last portion of a domain name.
for example, if the URL is http://www.telefonica.es/index.php, I'd
like to get the "es" code into a variable.

Is there a predefined call for this?
Otherwise, what string manipulation libraries are available for this?

Thanks for any help,

Laz.
 
L

Lee

Laz said:
Hi,

I've not worked with javascript, and after searching could not find
example of this type of manipulation.

I'm trying to set a variable to the last portion of a domain name.
for example, if the URL is http://www.telefonica.es/index.php, I'd
like to get the "es" code into a variable.

Is there a predefined call for this?
Otherwise, what string manipulation libraries are available for this?


var countryCode=location.host.replace(/^.*\./,"");

will give you whatever comes after the last "." in the domain name,
which may or may not be a country code.
 
B

Brian Genisio

Lee said:
Laz said:




var countryCode=location.host.replace(/^.*\./,"");

will give you whatever comes after the last "." in the domain name,
which may or may not be a country code.

Yeah, the OP should note that this method (and any other method) will
not always work. If it ends in .com or .edu or something like that,
then there is no country domain at the end to get, and you will get com
and edu.

Also, it will not work when running locally, off your drive, since there
is no location.host variable set. It also will not work on servers
without domain names... for instance, http://localhost/test.html or
http://SomePuterOnMyLocalNet/test.html will returh the host name in this
case, where the country code is not gotten, making testing a bit tough.

Just some thoughts,
Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top