`

javascript在IE和firefox里的中文兼容问题

阅读更多

针对firefox与IE对中文的编码处理不一致,IE总是以ISO-8859-1编码,而firefox则是以GB2312或者UTF8,所以可以采用如下方法来兼容IE和Firefox。

首先对得到到字符串进行编码判断,如果是ISO-8859-1,则进行编码转换,否则就不用转换了。

 

判断编码的方法如下:

public static String getEncoding(String str) {
       String encode = "GB2312";
       try {
           if (str.equals(new String(str.getBytes(encode), encode))) {
               String s = encode;
               return s;
           }
       } catch (Exception exception) {
       }
       encode = "ISO-8859-1";
       try {
           if (str.equals(new String(str.getBytes(encode), encode))) {
               String s1 = encode;
               return s1;
           }
       } catch (Exception exception1) {
       }
       encode = "UTF-8";
       try {
           if (str.equals(new String(str.getBytes(encode), encode))) {
               String s2 = encode;
               return s2;
           }
       } catch (Exception exception2) {
       }
       encode = "GBK";
       try {
           if (str.equals(new String(str.getBytes(encode), encode))) {
               String s3 = encode;
               return s3;
           }
       } catch (Exception exception3) {
       }
       return "";
   }

 

 

分享到:
评论
1 楼 powerclark 2009-12-18  
不错的处理方式~~~
中文相关的字段都可以采用这种方式来处理!

相关推荐

Global site tag (gtag.js) - Google Analytics