關於JS的編碼轉換問題

來源:互聯網
上載者:User

標籤:js   開發   編碼   報表   finereport   

在進行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.cptname="+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>


本文出自 “雄霸天下的部落格” 部落格,轉載請與作者聯絡!

關於JS的編碼轉換問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.