PDA

View Full Version : Make since of this script.



slambam
10-13-2006, 07:31 PM
Could someone please explain to me how this script works.

I would like to add this language changing drop down menu from http://www.google.com/support/webmasters to my website.

I looked at the code to try to make since of where it is getting the languages and I can not figure out how it works. Is it another page hidden somewhere or does it have something to do with the "en_GB" codes? I tried cutting and pasting what I thought I needed and the language will not change. I do not get any error, the page simply reloads to english and places a cookie on my computer. I am a total beginner with Javascript and could use some help.

Here the code that I copied to my site between the head tags:



<script type="text/javascript">
<!--
function correctLangCode_default(lang){
s = window.location;
s = s.toString();

if (lang == "zh-CN")
correctedLang = "zh_CN";
else if (lang == "zh-CN_CN")
correctedLang = "zh_CN";
else if (lang == "zh-TW")
correctedLang = "zh_TW";
else if (lang == "zh-TW_TW")
correctedLang = "zh_TW";
else if (lang == "pt-BR")
correctedLang = "pt_BR";
else if (lang == "pt-BR_BR")
correctedLang = "pt_BR";
else if (lang == "en-GB")
correctedLang = "en_GB";
else if (lang == "en-GB_GB")
correctedLang = "en_GB";
else if (lang == "en-US")
correctedLang = "en_US";

s = s.replace(lang, correctedLang);
location.replace(s);

return correctedLang;
}

function setCookies_default(cookieName,cookieValue,calledFrom) {
if (cookieValue == "") {
var cookieValue = getQueryVariable_default("hl");
}
//added because most Google products uses dashes for langs
if (cookieValue == "zh-TW"){
correctLangCode_default('zh-TW');
cookieValue = 'zh_TW';
}
if (cookieValue == "zh-TW_TW"){
correctLangCode_default('zh-TW');
cookieValue = 'zh_TW';
}
if (cookieValue == "zh-CN"){
correctLangCode_default('zh-CN');
cookieValue = 'zh_CN';
}
if (cookieValue == "zh-CN_CN"){
correctLangCode_default('zh-CN');
cookieValue = 'zh_CN';
}
if (cookieValue == "pt-BR"){
correctLangCode_default('pt-BR');
cookieValue = 'pt_BR';
}
if (cookieValue == "pt-BR_BR"){
correctLangCode_default('pt-BR');
cookieValue = 'pt_BR';
}
if (cookieValue == "en-GB"){
correctLangCode_default('en-GB');
cookieValue = 'en_GB';
}
if (cookieValue == "en-GB_GB"){
correctLangCode_default('en-GB');
cookieValue = 'en_GB';
}
if (cookieValue == "en-US"){
correctLangCode_default('en-US');
cookieValue = 'en_US';
}
if (cookieValue) {
var today = new Date();
var expire = new Date();
expire.setTime(today.getTime() + 3600000*24*365);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString()
+ ";path=/";
}
if (calledFrom == "dropdown") {
newURL = new String(window.location);
newURL = newURL.replace("hl=", "hlrm=");
window.location.href = newURL;
}
}
function getQueryVariable_default(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
-->
</script>

Here is the drop down menu placed in the body.


<form method="post" name="changelang" action="javascript:setCookies_default ('WebmasterLocale',this.document.changelang.lang.value, 'dropdown')" style="padding:0;margin:0;"><font style="font-size:10px ">Change Language:
<select name="lang" onchange="this.form.submit()" style="font-size:10px">
<option value="da" >Dansk</option>
<option value="de" >Deutsch</option>
<option value="en" selected>English</option>
<option value="en_GB" >English (UK)</option>
<option value="es" >Espa&#241;ol</option>
<option value="fi" >Suomi</option>
<option value="fr" >Fran&#231;ais</option>
<option value="it" >Italiano</option>
<option value="nl" >Nederlands</option>
<option value="no" >Norsk</option>
<option value="pl" >Polski</option>
<option value="pt_BR" >Portugu&#234;s</option>
<option value="sv" >Svenska</option>
<option value="ru" >Русский</option>
<option value="zh_CN" >中文(简体)</option>
<option value="zh_TW" >中文(繁體)</option>
<option value="ja" >日本語 </option>
<option value="ko" >한국어</option>
</select></font>
</form>

Any help would be great.:)

Twey
10-13-2006, 07:54 PM
No, it redirects to a different page. This is a very bad idea -- content negotiation should be performed on the server-side instead.

slambam
10-13-2006, 08:00 PM
Thank you for the information. For further learning, what part of the script is the other page? I do not see any other page names so I do not recognize the page right away. A little guidance would be appreciated. Also, how do they keep it from showing in the address bar?

I will search the web for server side includes and hopefully my quest will lead me in the right direction. Thank you again Twey for the help.

Twey
10-13-2006, 08:02 PM
s = window.location;
// ...
s = s.replace(lang, correctedLang);
location.replace(s);

slambam
10-13-2006, 08:47 PM
thank you. Have a good weekend. :)