Extracting just the country code from URL?

Discussion in 'Javascript' started by Laz, Jan 20, 2004.

  1. Laz

    Laz Guest

    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.
    Laz, Jan 20, 2004
    #1
    1. Advertising

  2. Laz

    Lee Guest

    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.
    Lee, Jan 20, 2004
    #2
    1. Advertising

  3. 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
    Brian Genisio, Jan 20, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon paugh
    Replies:
    1
    Views:
    660
  2. S.Guhananth
    Replies:
    1
    Views:
    530
    Alvin Bruney [Microsoft MVP]
    Apr 30, 2005
  3. Zyron
    Replies:
    2
    Views:
    1,478
    Jukka K. Korpela
    Nov 8, 2005
  4. Just D.
    Replies:
    0
    Views:
    392
    Just D.
    Aug 11, 2004
  5. Petr Janda
    Replies:
    2
    Views:
    142
    rretzbach
    Mar 8, 2007
Loading...

Share This Page