<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    wuxiren123

    關(guān)于JS的編碼轉(zhuǎn)換問題

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

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

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

    javascript中FineReport字符轉(zhuǎn)換原理

    在給報表服務(wù)器發(fā)送請求之前,對URL或者只對URL里面的參數(shù)名字和參數(shù)值,進行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;     
    }


     

    經(jīng)過編碼的URL或者Form表單,報表服務(wù)器智能的將這些字符正確的轉(zhuǎn)換過來。

    cjkEncode方法在FineReport的JS庫中已經(jīng)預(yù)先提供了,用戶只要加載了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&地區(qū)=華東");       
    }
           
    </Script>
    </head>
    <body>
    <input type="button" value="字符轉(zhuǎn)換1" onclick="frOpen()">
    </body>
    </html>


    如果只對參數(shù)值進行編輯轉(zhuǎn)換,在參數(shù)后面調(diào)用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');     //獲取到參數(shù)Region所在文本框
        Region1.value = FR.cjkEncode(Region.value);         //對值參數(shù)值進行編碼轉(zhuǎn)化
        Region1.name = FR.cjkEncode("地區(qū)");               //對參數(shù)控件名編碼轉(zhuǎn)換,如果參數(shù)名字為英文,則不需要此操作
        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="地區(qū)" value="華東">
    <input type="button" name="show" value= "查看" onclick="autoSubmit()"/>
    </body>
    </html>


    3、特殊符號處理

    如果在需要進行cjkEncode的URI的參數(shù)中包含特殊字符,比如%,#,$,=,&,/,?,+,@等字符時,需要在cjkEncode之后,再次調(diào)用javascript的encodeURIComponent對這些特殊字符進行編碼。如參數(shù)值是”%華%“這樣的字符,就需要寫成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&地區(qū)="+encodeURIComponent(FR.cjkEncode("%華%"));      
        }
           
    </Script>
    </head>
    <body>
    <input type="button" value="字符轉(zhuǎn)換1" onclick="frOpen()">
    </body>
    </html>

     

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


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产黄在线观看| 亚洲精品免费观看| 中文字幕免费人成乱码中国| 亚洲AV永久纯肉无码精品动漫| 国产精品视频免费| 男人j进女人p免费视频| 五月天网站亚洲小说| 四虎成人免费观看在线网址 | 最近最新高清免费中文字幕 | 一个人免费视频在线观看www| 亚洲最大黄色网站| 亚洲AⅤ永久无码精品AA | 国产福利免费观看| 一个人免费视频在线观看www| 一区二区亚洲精品精华液| 亚洲国产主播精品极品网红| 亚洲精品在线免费观看| 人妻18毛片a级毛片免费看| 亚洲国产亚洲片在线观看播放| 亚洲一区无码精品色| 麻豆一区二区免费播放网站 | 免费无码又爽又刺激一高潮| 学生妹亚洲一区二区| 久久99国产亚洲高清观看首页| 毛片a级毛片免费播放100| a级片免费在线播放| 久久久亚洲精华液精华液精华液| 亚洲av丰满熟妇在线播放| 亚洲国产综合精品一区在线播放| 国产在线jyzzjyzz免费麻豆| 久久久久女教师免费一区| 亚洲AV日韩综合一区| 亚洲日产2021三区在线| 国产亚洲精品精华液| 亚洲精品国产精品乱码不卡| 免费无码黄动漫在线观看| 69堂人成无码免费视频果冻传媒 | 亚洲精品视频免费| 亚洲av无码偷拍在线观看| 亚洲免费黄色网址| 亚洲综合色丁香麻豆|