PDA

View Full Version : Javascript: Get Country


sumeet
03-10-2006, 06:46 PM
I'm trying to automatically redirect users based on what country they are visiting from. Do any of you know of any scripts that will return a user's country? From there, it's very easy to do a JavaScript redirect based on the value of the country.

So, where can I find a simple JS include that returns a user's country?

Philip M
03-10-2006, 07:06 PM
As far as I know this is not practicable.

See: http://software77.net/cgi-bin/ip-country/geo-ip.pl

This is a database of 2,861,537,958 IP numbers in use on the internet today,
but there is no reliable method of relating blocks of IP numbers to countries.

_Aerospace_Eng_
03-10-2006, 07:10 PM
And JS can't detect IP addresses either. You need a server side language for that.

sumeet
03-10-2006, 07:42 PM
Thanks for the replies. I did some Googling, and this site seems to be what I need:

http://www.geobytes.com/GeoDirection.htm

How is it possible that they can offer such a service is JS can't detect IPs?

_Aerospace_Eng_
03-10-2006, 07:54 PM
They are using a server side language to get the IPs, there script interacts with their server some how.

NeoPuma
03-10-2006, 08:39 PM
I use php to sort out mine. Its an easy bit of code, and uses browsers settings to detect where people live. I use mine for language files but it can be used for country - try googling it.

Kor
03-10-2006, 09:32 PM
even so, people might hack their IP using either a self created application or even some more or less public"anonymizer" sites... This is not an "absoute" way to hide your "tracks" but for a less skilled programmer (or a common server-side apoplication - I mean an aplication which does not request for a real confirm of the IP) , it is hard to find the real IP...

sumeet
03-10-2006, 10:33 PM
Is it at all possible to determine the browser's language settings via JavaScript? If it is, then couldn't server-side scripting be avoided completelet?

_Aerospace_Eng_
03-10-2006, 10:34 PM
No its not possible. I'm telling you their JS script interacts with the server then gets then sends the variables to the script before its outputted. Okay so apparently it used to be possible but it only worked for Netscape browsers.
<script type="text/javascript">
<!-- Begin

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

if ((navigator.appVersion.indexOf("4.") != -1) && (navigator.appName.indexOf("Netscape") != -1)){
ip = "" + java.net.InetAddress.getLocalHost().getHostAddress();
document.write("Your IP address is " + ip);
}
else {
document.write("IP Address only shown in Netscape with Java enabled!");
}
// End -->
</script>

jalarie
03-16-2006, 03:47 PM
Is it at all possible to determine the browser's language settings via JavaScript? If it is, then couldn't server-side scripting be avoided completelet?

if (navigator.language) { Lang=navigator.language }
else { Lang=navigator.userLanguage }

felgall
03-17-2006, 12:29 AM
See http://javascript.about.com/library/bllang.htm for a script that will automatically redirect to the version of a page written in the visitors language (if it is available).