| 導購 | 订阅 | 在线投稿
分享
 
 
 

javascript 錯誤「缺少十六進制數字」的處理

來源:互聯網  2008-10-21 08:21:18  評論

原因:JS提交數據時出現特殊符號"\",javascript中"\"是個特殊的字符,在很多場合需要轉換。

參考:

例子程序:

*Encode for HTML.

*/

public static String htmlEncoder(String str)

{

if(str==null || str.equals(""))

return "";

String res_str;

res_str=strReplace(str,"&","&");

res_str=strReplace(str," "," ");

res_str=strReplace(str,"<","&lt;");

res_str=strReplace(str,">","&rt;");

res_str=strReplace(str,"\"","&quot;");

res_str=strReplace(str,"'","'");

return res_str;

}

/** *//**

*Encode for HTML-Text.

*/

public static String htmlTextEncoder(String str)

{

if(str==null || str.equals(""))

return "";

String res_str;

res_str=strReplace(str,"&","&amp;");

res_str=strReplace(str,"<","&lt;");

res_str=strReplace(str,">","&rt;");

res_str=strReplace(str,"\"","&quot;");

res_str=strReplace(str,"'","'");

res_str=strReplace(str," ","&nbsp;");

res_str=strReplace(str,"\r\n","<br/>");

res_str=strReplace(str,"\r","<br/>");

res_str=strReplace(str,"\n","<br/>");

return res_str;

}

/** *//**

*Encode for URL.

*/

public static String urlEncoder(String str) {

return java.net.URLEncoder.encode(str) ;

}

/** *//**

*Encode for XML.

*/

public static String xmlEncoder(String str)

{

if(str==null || str.equals(""))

return "";

String res_str;

res_str=strReplace(str,"&","&amp;");

res_str=strReplace(res_str,"<","&lt;");

res_str=strReplace(res_str,">","&gt;");

res_str=strReplace(res_str,"\"", "&quot;");

res_str=strReplace(res_str,"\'", "&acute;");

return res_str;

}

/** *//**

*Encode for SQL.

*/

public static String sqlEncoder(String str)

{

if(str==null || str.equals(""))

return "";

String res_str;

res_str=strReplace(str,"'","''");

return res_str;

}

/** *//**

*Encode for Javascript.

*/

public static String jsEncoder(String str)

{

if(str==null || str.equals(""))

return "";

String res_str;

res_str=strReplace(str,"'","\'");

res_str=strReplace(str,"\"","\\\"");

res_str=strReplace(str,"\r\n","\\\n");

res_str=strReplace(str,"\n","\\\n");

res_str=strReplace(str,"\r","\\\n");

return res_str;

}

html=html+replace(table_list(fileExt,path,2),"\","/")+""""

原因:JS提交數據時出現特殊符號"\",javascript中"\"是個特殊的字符,在很多場合需要轉換。 參考: 例子程序: *Encode for HTML. */ public static String htmlEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"&","&amp;"); res_str=strReplace(str," ","&nbsp;"); res_str=strReplace(str,"<","&lt;"); res_str=strReplace(str,">","&rt;"); res_str=strReplace(str,"\"","&quot;"); res_str=strReplace(str,"'","'"); return res_str; } /** *//** *Encode for HTML-Text. */ public static String htmlTextEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"&","&amp;"); res_str=strReplace(str,"<","&lt;"); res_str=strReplace(str,">","&rt;"); res_str=strReplace(str,"\"","&quot;"); res_str=strReplace(str,"'","'"); res_str=strReplace(str," ","&nbsp;"); res_str=strReplace(str,"\r\n","<br/>"); res_str=strReplace(str,"\r","<br/>"); res_str=strReplace(str,"\n","<br/>"); return res_str; } /** *//** *Encode for URL. */ public static String urlEncoder(String str) { return java.net.URLEncoder.encode(str) ; } /** *//** *Encode for XML. */ public static String xmlEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"&","&amp;"); res_str=strReplace(res_str,"<","&lt;"); res_str=strReplace(res_str,">","&gt;"); res_str=strReplace(res_str,"\"", "&quot;"); res_str=strReplace(res_str,"\'", "&acute;"); return res_str; } /** *//** *Encode for SQL. */ public static String sqlEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"'","''"); return res_str; } /** *//** *Encode for Javascript. */ public static String jsEncoder(String str) { if(str==null || str.equals("")) return ""; String res_str; res_str=strReplace(str,"'","[url=file://\\]\\'[/url]"); res_str=strReplace(str,"\"","\\\""); res_str=strReplace(str,"\r\n","\\\n"); res_str=strReplace(str,"\n","\\\n"); res_str=strReplace(str,"\r","\\\n"); return res_str; } html=html+replace(table_list(fileExt,path,2),"\","/")+""""
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有