wuxiren123

          關于JS的編碼轉換問題

          在進行JS開發過程中,尤其是在開發報表時,報表已集成到Web頁面中,通過在頁面傳遞參數至報表中時,會發現有時某些參數值,傳遞到報表中是顯示為問號或亂碼等等一系列不能正常顯示的情況。

          這是由于瀏覽器和報表服務器的編碼不同,字符多次進行編碼轉換時出現錯誤導致字符的顯示出現亂碼,尤其是中日韓文和特殊字符更容易出現亂碼問題。

          以開發報表軟件FineReport為例,在給報表服務器發送請求之前,對URL或者只對URL里面的參數名字和參數值,進行cjkEncode的編碼,該方式兼容了各種不同的字符集,如ISO8859-1、 UTF-8、 GBK、 ENU_JP,尤其對中日韓文的處理采取了統一的方案。

          javascript中FineReport字符轉換原理

          在給報表服務器發送請求之前,對URL或者只對URL里面的參數名字和參數值,進行cjkEncode的編碼。源碼如下:

          function cjkEncode(text) {     
              
          if (text == null{     
                  
          return "";     
              }
               
              
          var newText = "";     
              
          for (var i = 0; i < text.length; i++{     
                  
          var code = text.charCodeAt (i);      
                  
          if (code >= 128 || code == 91 || code == 93{//91 is "[", 93 is "]".     
                      newText += "[" + code.toString(16+ "]";     
                  }
           else {     
                      newText 
          += text.charAt(i);     
                  }
               
              }
               
              
          return newText;     
          }


           

          經過編碼的URL或者Form表單,報表服務器智能的將這些字符正確的轉換過來。

          cjkEncode方法在FineReport的JS庫中已經預先提供了,用戶只要加載了FR的JS庫,就可以使用FR.cjkEncode對中日韓文字符進行encode,如下示例:

          1、  對URL進行cjkEncode

          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=GBK">
          <script type="text/javascript"    src="ReportServer?op=emb&resource=finereport.js"></script>
          <Script Language="JavaScript">             
          function frOpen() {   
              window.location
          =FR.cjkEncode("http://localhost:8075/WebReport/ReportServer?reportlet=doc/Primary/Parameter/Parameter.cpt&地區=華東");       
          }
                 
          </Script>
          </head>
          <body>
          <input type="button" value="字符轉換1" onclick="frOpen()">
          </body>
          </html>


          如果只對參數值進行編輯轉換,在參數后面調用FR.cjkEncode()方法,如:

          window.location="http://localhost:8075/WebReport/ReportServer?reportlet=reportname.cpt¶name="+FR.cjkEncode("華東"); 

          2、對Form表單進行cjkEncode

          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
          <script type="text/javascript" src="/WebReport/ReportServer?op=emb&resource=finereport.js"></script>
          <script>
          function autoSubmit() {
              
          var Region1 = document.getElementById('Region');     //獲取到參數Region所在文本框
              Region1.value = FR.cjkEncode(Region.value);         //對值參數值進行編碼轉化
              Region1.name = FR.cjkEncode("地區");               //對參數控件名編碼轉換,如果參數名字為英文,則不需要此操作
              document.FRform.submit();
          }

          </script>
          <body>
          <form name=FRform method=post action="/WebReport/ReportServer?reportlet=doc/Primary/Parameter/Parameter.cpt">
          <input type="text" id="Region" name="地區" value="華東">
          <input type="button" name="show" value= "查看" onclick="autoSubmit()"/>
          </body>
          </html>


          3、特殊符號處理

          如果在需要進行cjkEncode的URI的參數中包含特殊字符,比如%,#,$,=,&,/,?,+,@等字符時,需要在cjkEncode之后,再次調用javascript的encodeURIComponent對這些特殊字符進行編碼。如參數值是”%華%“這樣的字符,就需要寫成encodeURIComponent(FR.cjkEncode("%華%")),一定要先進行cjkEncode,然后再進行encodeURIComponent,完整代碼如下:

          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=GBK">
          <script type="text/javascript"    src="ReportServer?op=emb&resource=finereport.js"></script>
          <Script Language="JavaScript">             
          function frOpen() {   
          window.location
          =FR.cjkEncode("http://localhost:8075/WebReport/ReportServer?reportlet=doc/Primary/Parameter/Parameter.cpt&地區="+encodeURIComponent(FR.cjkEncode("%華%"));      
              }
                 
          </Script>
          </head>
          <body>
          <input type="button" value="字符轉換1" onclick="frOpen()">
          </body>
          </html>

           

          posted on 2016-08-11 15:26 喝水居然長肉 閱讀(96) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 石渠县| 平谷区| 民县| 喀喇| 华坪县| 芦山县| 含山县| 株洲县| 大连市| 安溪县| 宁津县| 修文县| 大姚县| 德阳市| 象州县| 邹平县| 鸡东县| 伽师县| 文昌市| 衡山县| 金山区| 伊吾县| 乐至县| 浦城县| 青河县| 汉寿县| 台东市| 德化县| 嘉峪关市| 中牟县| 师宗县| 普陀区| 获嘉县| 祁阳县| 司法| 耿马| 涿鹿县| 辽宁省| 荔浦县| 汉源县| 上蔡县|