Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

12 of 17 people (71%) answered Yes
Recently 7 of 10 people (70%) answered Yes


How can I redirect according to browser country. I.e. like sites with .com send you to ?

Apr 5th, 2008 20:20
ha mo, Jean-Bernard Valentaten, Gareth Hay,

By using the navigator.language property you can set up a redirection-
function redirect()
  var lang = navigator.language;
  var retString = 'http://www.yourUrl.';
  if (lang = 'en')
    retString += 'com';
    retString += lang;
  return retString;
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 
German language).
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.