wuxiren123

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          關于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 喝水居然長肉 閱讀(97) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 常德市| 社旗县| 施甸县| 子洲县| 城口县| 崇阳县| 青冈县| 湖南省| 和硕县| 乌拉特前旗| 方山县| 崇义县| 武冈市| 秦安县| 时尚| 赤城县| 高清| 九龙县| 息烽县| 鹰潭市| 黑山县| 南投市| 宣威市| 平阳县| 土默特右旗| 自贡市| 台前县| 洱源县| 东海县| 通州区| 名山县| 卢龙县| 泊头市| 海伦市| 应城市| 措美县| 盐池县| 南京市| 鹰潭市| 云梦县| 全椒县|