Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Annoying browser redirect problem 1

Status
Not open for further replies.

LaPluma

Programmer
Feb 3, 2002
139
DE
Hello

I have a piece of JavaScript code here (which somebody on the Tek-Tips forum kindly helped me out with):

<SCRIPT language=&quot;javascript&quot;>

function getlanguage(){

language=navigator.language;

if(language) {language=language.substring(0,2);}

else {language=&quot;z&quot;;}

switch(language) {
case 'en' : window.location =
' case 'de' : window.location =
' default : window.location =
' }
}

</SCRIPT>

The site, I am concerned about has a Russian language home page, but I wish to redirect visitors who are viewing the site from an English language browser (Netscape or Explorer) to an English version of the Russian site.

The two pages above, that is,

and

are test pages, but represent the Russian home page and the English version respectively.

The problem is that the redirection doesn't work (at least, not for me: I have visited the Russian version through IE6).

Does anybody know why this script isn't working?

A Russian colleague of mine suggests that the script isn't working &quot;because the function getlanguage() isn't called in
the Web page&quot;. He also suggests that the script should make use of a variable which can identify the browser's language.

Incidentally, what does the following do:

language=navigator.language;

Any help would be most appreciated.

Best wishes

LaPluma
 
Replace
language=navigator.language;

With

if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

and make sure you call the getlanguage() function somewhere on your page Adam
 
Hello Adam101

Many thanks for your post.

Does the

if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

suggest that the code is more suitable for Netscape? I have yet to try it out in Netscape.

Many thanks

LaPluma
 
No, the if (navigator.appName == 'Netscape') detects whether or not the browser the visitor is using is Netscape. If it is, it says to assign the language property of the navigator object to the variable &quot;language&quot;. If it's not Netscape, then it's probably Internet Explorer. Internet Explorer's navigator object doesn't have a language property, it has a browserLanguage property. So it assigns that to the variable &quot;language&quot; instead. Adam
 
Hello Adam (again)

Thank you again for your post and a hearty thanks for your explanation. In particular, I didn't know that Explorer's property was 'browserLanguage' (that was why I was beginning to think: &quot;where does IE fit in to this?&quot;).

Again, much appreciated!

Best wishes

LaPluma
 
Hello Adam

This is what I finally have:

<html>
<head>
<title>Test Language</title>

<SCRIPT language=&quot;javascript&quot;>

function getlanguage(){

if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;


if(language) {
language=language.substring(0,2);
}
else {
language=&quot;z&quot;;
}

switch(language) {
case 'en' : window.location =
' case 'de' : window.location =
' default : window.location =
'}

}

</SCRIPT>
</head>

<body onLoad='getlanguage();'>

</body>
</html>

Does that look reasonable to you?

Thanks again

LaPluma
 
Many thanks Adam for all your help, I'm grateful.

LaPluma
 
Here is more complete and correct info:


browser | property used | value returned
-----------------------------------------------

Opera | navigator.language | en

Netscape 4.x | navigator.language | en

IE | navigator.browserLanguage | en-us

Mozilla/N6+ | navigator.language | en-US

As you see, all browsers use &quot;navigator.language&quot; except IE (that use &quot;navigator.browserLanguage&quot;).
Therefore, if you want to cover as many browsers as possible, the script should be modified to this:

if (navigator.appName == 'Microsoft Internet Explorer')
var language = navigator.browserLanguage
else
var language = navigator.language;
 
Hello Starway

Thank you for your post.

Basically you are allowing for other browsers (other than Netscape and IE, I mean), so that's useful.

I'll incorporate it into what I already have - many thanks!

Best wishes

LaPluma
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top