我要啦免费统计

          微藍(lán)領(lǐng)域

          我的學(xué)習(xí)檔案館
          posts - 19, comments - 57, trackbacks - 0, articles - 57
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          javascript特殊字符轉(zhuǎn)換

          Posted on 2007-10-18 11:53 hilor 閱讀(4000) 評(píng)論(0)  編輯  收藏 所屬分類: common
          javascript和JAVA一樣,一些特殊字符需要轉(zhuǎn)義
           
          特別是在一些JSP網(wǎng)頁(yè)的開(kāi)發(fā)中,好多程序員經(jīng)常會(huì)忘掉這點(diǎn),
          例:
          1 <% List textList = (List)request.getAttribute("textList"); %>
          2 <script>
          3 <!--
          4 var txtList = new Array();
          5 <% for ( int i = 0 ; i < textList.size() ; i++) { %>
          6 txtList[<%=i%>= "<%=textList.get(i)%>";
          7 <% } %>
          8 -->
          9 </script>
           
          這段JS就存在問(wèn)題,未對(duì)特殊符號(hào)進(jìn)行處理.有特殊符號(hào)的情況下有可能報(bào)JSERROR
           
          JAVASCRIPT中需要轉(zhuǎn)義的有:
           
          轉(zhuǎn)義序列 字符
          \b 退格
          \f 走紙換頁(yè)
          \n 換行
          \r 回車
          \t 橫向跳格 (Ctrl-I)
          \' 單引號(hào)
          \" 雙引號(hào)
          \\ 反斜杠

          此外,對(duì)/符號(hào)我覺(jué)得也有必要進(jìn)行處理,因?yàn)橄?--></script> 這樣的字符串也會(huì)使SCRIPT出錯(cuò).
           
          下面提供一個(gè)比較實(shí)用java的方法,做這個(gè)特殊符號(hào)的處理:
           1public class JavaScriptUtils {
           2 public static String javaScriptEscape(String input) {
           3  if (input == null{
           4   return input;
           5  }

           6  StringBuffer filtered = new StringBuffer(input.length());
           7  char prevChar = '\u0000';
           8  char c;
           9  for (int i = 0; i < input.length(); i++{
          10   c = input.charAt(i);
          11   if (c == '"'{
          12    filtered.append("\\\"");
          13   }

          14   else if (c == '\'') {
          15    filtered.append("\\'");
          16   }

          17   else if (c == '\\'{
          18    filtered.append("\\\\");
          19   }

          20   else if (c == '\t'{
          21    filtered.append("\\t");
          22   }

          23   else if (c == '\n'{
          24    if (prevChar != '\r'{
          25     filtered.append("\\n");
          26    }

          27   }

          28   else if (c == '\r'{
          29    filtered.append("\\n");
          30   }
           else if (c == '\f'{
          31                filtered.append("\\f");
          32  }
           else if (c == '/'{
          33                filtered.append("\\/");
          34            }

          35   else {
          36    filtered.append(c);
          37   }

          38   prevChar = c;
          39  }

          40  return filtered.toString();
          41 }

          42}
          43


          上面的例子應(yīng)改為:
          1 <% List textList = (List)request.getAttribute("textList"); %>
          2 <script>
          3 <!--
          4 var txtList = new Array();
          5 <% for ( int i = 0 ; i < textList.size() ; i++) { %>
          6 txtList[<%=i%>= "<%=JavaScriptUtils.javaScriptEscape(textList.get(i))%>";
          7 <% } %>
          8 -->
          9 </script>
          主站蜘蛛池模板: 田阳县| 原平市| 莒南县| 陇西县| 崇义县| 凤山县| 岗巴县| 昭觉县| 武宣县| 沂水县| 将乐县| 北川| 柳州市| 图们市| 和田县| 仁怀市| 灵石县| 略阳县| 菏泽市| 温泉县| 民权县| 塔城市| 本溪市| 桂林市| 青河县| 长海县| 应城市| 新邵县| 紫阳县| 伽师县| 武川县| 涿州市| 高碑店市| 沂水县| 普格县| 吴川市| 泗水县| 兴宁市| 南丰县| 永吉县| 岚皋县|