469,609 Members | 1,179 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Extracting just the country code from URL?

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.
Jul 20 '05 #1
2 3123
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.

Jul 20 '05 #2
Lee wrote:
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.


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

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Michael Hill | last post: by
4 posts views Thread by Susanne Christe | last post: by
2 posts views Thread by bjm | last post: by
6 posts views Thread by Mag Gam | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.