Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

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

Entry

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-
skript:
function redirect()
{
  var lang = navigator.language;
  var retString = 'http://www.yourUrl.';
  if (lang = 'en')
    retString += 'com';
  else
    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.
HTH
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com