Results 1 to 5 of 5

Thread: Make since of this script.

  1. #1
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Make since of this script.

    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:

    Code:
    <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.

    Code:
    <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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, it redirects to a different page. This is a very bad idea -- content negotiation should be performed on the server-side instead.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
       s = window.location;
       // ...
       s = s.replace(lang, correctedLang);
       location.replace(s);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you. Have a good weekend.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •