jquery填充table时怎么处理特殊字符?
查询的数据中存在特殊字符或者js语句,如:数据库字段中有一个字段的内容是: "alert('商品编码');",那么这个字段查出来到表格中之后,页面会弹出提示框
2.grid查询的字段来自于用户手动输入的文本,如果有恶意攻击,直接输入js语句,会执行相关语句。
表格字段formatter 的时候调用 HTMLEncode
{field:'dlAddress',title:'使用地点',width: 200,align:'center',
formatter : function(value, row, index) {
return HTMLEncode(value);
}
}
/*-----------------------------------------------------------------------------------------*\
* 函数: 把特殊字符进行转换
* 参数: value -- 需要转化的字符串
* 返回值:
* 描述:
\*-----------------------------------------------------------------------------------------*/
function HTMLEncode(value) {
var returnValue;
if(value==null){
return null;
}
returnValue = value.replace(/&/g, '&');
returnValue = returnValue.replace(/</g, '<');
returnValue = returnValue.replace(/>/g, '>');
returnValue = returnValue.replace(/\n\n/g, '<br/>');
returnValue = returnValue.replace(/\r\r/g, '<br/>');
returnValue = returnValue.replace(/\n/g, '<br/>');
returnValue = returnValue.replace(/\r/g, '<br/>');
returnValue = returnValue.replace(/\t/g, ' ');
return returnValue;
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有