Showing script based on the country

G

Guest

I have a few javascripts. I would like to show a certain script based on
the country ip of the viewer, how would I achieve this? is there html I
could use to run the javascript based on the country ip?
 
J

Jerry Stuckle

Nospam said:
I have a few javascripts. I would like to show a certain script based on
the country ip of the viewer, how would I achieve this? is there html I
could use to run the javascript based on the country ip?

You can't do it reliably. All you can do is take a guess at the country of the
ip connecting to you, and that won't always be accurate. Plus they might be
using a proxy anyplace in the world.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
(e-mail address removed)
==================
 
M

Mark Goodge

You can't do it reliably. All you can do is take a guess at the country of the
ip connecting to you, and that won't always be accurate. Plus they might be
using a proxy anyplace in the world.

That's true, but it's not quite as bad as you make it sound. Freely
available Geo-IP databases will give over 95% accuracy, which is
usually enough for most purposes. Proxies aren't really a major issue,
as almost all well-configured proxies will pass the originating IP on
in the HTTP-FORWARDED-FOR header. Those that don't are generally those
which are deliberately designed to anonymise the origin, and that's
fairly rare.

The main problem is AOL, which will always appear as the US
irrespective of the user's actual location, as AOL configures all its
worldwide addresses from the same US-allocated pool. To get accurate
geo-location of AOL users, therefore, you need access to AOL's
internal database, and that isn't available for free. Some commercial
products include it (as well as other ISP cross-border databases), and
with that level of detail you can get over 99% accuracy - provided
you're prepared to pay for it.

To go back to the OP's question, Geo-IP detection is best done
server-side using a combination of scripting and a database, as you're
going to need to cross-check against a quite large amount of data.
It's pretty trivial with most server-side scripting/database
combinations, such as PHP/MySQL or ASP/MSSQL, but you do need a
certain familiarity with these in order to use them. If you don't want
to run your own database, I'd suggest using the freeware binary data
from MaxMind, together with one of their supplied APIs in the
scripting language of your choice:

http://www.maxmind.com/app/geoip_country

Mark
 
N

Neredbojias

To further the education of mankind, Jerry Stuckle
You can't do it reliably. All you can do is take a guess at the
country of the ip connecting to you, and that won't always be
accurate. Plus they might be using a proxy anyplace in the world.

Yeah. One time I used a proxy in Biloxi to chat with some foxy with the
moxie in Bayonne...
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Mon, 3 Jul 2006 22:22:08 remote, seen in
You can't do it reliably. All you can do is take a guess at the country of the
ip connecting to you, and that won't always be accurate. Plus they might be
using a proxy anyplace in the world.

Plus it may not be relevant. A substantial proportion of people
hereabouts, both in this district and in this building, are of non-UK
origin and may well prefer to be treated as if in another country.

Then many of those using Internet cafes are just passing through the
area.

If you are required to be selective, then javascript is not the tool,
and the task may be impossible; otherwise, let the user choose.
 
J

Jukka K. Korpela

Nospam said:
I have a few javascripts. I would like to show a certain script
based on the country ip of the viewer, how would I achieve this? is
there html I could use to run the javascript based on the country ip?

Why would you _show_ a certain script?

In almost all cases like this, an analysis of the problem reveals that it's
not really about country but language. So the problem definition is wrong,
and we don't even need to consider all the pitfalls in solving the wrong
problem.

In any case, telling the _real_, original problem and illustrating it with a
URL would at least give you a working chance of getting a useful solution.

F'ups trimmed.
 
J

jojo

Nospam said:
I have a few javascripts. I would like to show a certain script based on
the country ip of the viewer, how would I achieve this? is there html I
could use to run the javascript based on the country ip?

Another solution would be to write everything in one script but call
different functions according to the language. The advantage would be
that you can use JS to get the language selected in the browser:

var lang;
if (navigator.browserLanguage) lang=navigator.browserLanguage; //IE
else if (navigator.language) lang=navigator.language; //others

or, shortend:

var lang=(navigator.browserLanguage) ? navigator.browserLanguage :
navigator.language;
 
J

Jukka K. Korpela

jojo said:
Another solution would be to write everything in one script but call
different functions according to the language.

Solution to what? Surely not to the question that was asked, though perhaps
to the question that the OP _meant_ to ask. It's probably still the wrong
question.
The advantage would be
that you can use JS to get the language selected in the browser:

No, it is a definite _disadvantage_, since now you would not even try to
select the user's preferred language but the language of the browser's
interface. This language is very often English even though the user knows
just a little English and prefers quite other languages. The simple reason
is that many browsers (especially new versions) are available as English
versions only.
 
J

jojo

Jukka said:
Solution to what? Surely not to the question that was asked, though
perhaps to the question that the OP _meant_ to ask. It's probably still
the wrong question.


No, it is a definite _disadvantage_, since now you would not even try to
select the user's preferred language but the language of the browser's
interface. This language is very often English even though the user
knows just a little English and prefers quite other languages. The
simple reason is that many browsers (especially new versions) are
available as English versions only.
ASFAIK the JS-properties I suggested provide the language the user set
as his preferred language. In most browsers (IE, Firefox, Netscape,
Opera, ...) there is an option to select your preferred languages (_any_
language you like to, no matter in which language the browsers interface
is).
So it is even better using this properties than the IP because if, lets
say a German, is in the USA he has got an American IP. But perhaps he
still wants to view the websites in German...
 
J

Jukka K. Korpela

jojo <[email protected]> scripsit:

[ pointless fullquote - the usual signal of lack of comprehensive reading ]
ASFAIK the JS-properties I suggested provide the language the user set
as his preferred language.

"ASFAIK"? So you never actually tested it?
In most browsers (IE, Firefox, Netscape,
Opera, ...) there is an option to select your preferred languages
(_any_ language you like to, no matter in which language the browsers
interface is).

Yes. And this setting has, unfortunately, rather limited effect, since so
few servers try to make use of it. Besides, the setting is often wrong: it
reflects browser defaults rather than user preferences.

And the method you suggested does not fetch that setting but the browser's
interface language. Try it. Select any languages you like in the language
preferences and see what your code produces: not a string of language
preference list, or the first language in the list, or the language with the
highest quality factor. Instead, you get "en" if your browser interface is
English.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top