How can I redirect according to browser country. I.e. like sites with .com send you to .co.uk ?
Apr 5th, 2008 20:20
ha mo, Jean-Bernard Valentaten, Gareth Hay,
By using the navigator.language property you can set up a redirection-
var lang = navigator.language;
var retString = 'http://www.yourUrl.';
if (lang = 'en')
retString += 'com';
retString += lang;
This skript is based on the thought that the language shortcut is equal
to the top-level-domain you want to use (e.g. 'de' for Germany and
Of course there are countries that speak a language that is spoken in
other countries too (e.g. Austria is german speaking but has the
tld .at, Belgium is french and dutch speaking but has the tld .be and
so on), so you'll have to change skript any way you need it.
The redirection then works this way:
<body ... onload="window.location.href = redirect();">
By the way, any good programmer would tell you to use the switch
statement instead of a heck load of if ... else if ... statements.
This is basically correct, but will only work in IE. NN seems to have
the same restrictions as Java in the switch statement, it will only
parse characters, not strings. So in order to do a correct cross-
browser programming you'll have to use cascaded if ... statements.
This generates dirty code, but it's the fastest technique.