url传参时特殊字符的处理
由于“url?param” 传递的参数是通过get 方式提交的,所以会出现中文乱码,特殊字符无效等情况,这里只写出特殊字符的处理
关键:将idUser隐藏在jsp中,用集合下标number作为超链接idUser的标志,在js中通过选择器取值。同时在js中对该参数调用escapeStr方法,处理特殊字符
jsp:
<table>
<logic:iterate id="list" name="a_usr_s02Form" property="listOutput" indexId="number" >
<tr>
<td class="tdLeft">
<!--hidden url and param idUser -->
<html:hidden property="idUser" name="list" styleClass="urlparam${number}"/>
<input type="hidden" value="<%=request.getContextPath()%>/a_usr/a_usr_s02_UpdateBL.do?idUser=" class="urladdress"/>
<td class="tdLeft">
<!--hidden url and param idUser -->
<html:hidden property="idUser" name="list" styleClass="urlparam${number}"/>
<input type="hidden" value="<%=request.getContextPath()%>/a_usr/a_usr_s02_UpdateBL.do?idUser=" class="urladdress"/>
<a id="code" href="#" onclick="openDefinesWindow('url','<bean:write name="number"/>','UserAccount','<bean:message key="window.width"/>','<bean:message key="window.height" />','yes')"><bean:write name="list" property="idUser" /></a>
</td>
<td class="tdLeft"><bean:write name="list" property="userName" /></td>
<td class="tdLeft"><t:writeCodeValue codeList="LIST_USERTYPE" property="userType" name="list" /></td>
<td class="tdLeft"><bean:write name="list" property="svcCtrName" /></td>
<td class="tdLeft"><t:writeCodeValue codeList="LIST_USERSTATUS" property="userStatus" name="list" /></td>
</tr>
</logic:iterate>
</table>
<td class="tdLeft"><bean:write name="list" property="userName" /></td>
<td class="tdLeft"><t:writeCodeValue codeList="LIST_USERTYPE" property="userType" name="list" /></td>
<td class="tdLeft"><bean:write name="list" property="svcCtrName" /></td>
<td class="tdLeft"><t:writeCodeValue codeList="LIST_USERSTATUS" property="userStatus" name="list" /></td>
</tr>
</logic:iterate>
</table>
js
function openDefinesWindow(url,number,name,width,height,scrollbars) {
var features = "toolbar=no, status=yes, menubar=no, location=no,resizable=yes";
var left = Number((screen.availWidth/2) - (width/2));
var top = Number((screen.availHeight/2) - (height/2));
var strFeatures = features + ", width = "+width+", height = "+height+", top = "+top+", left = "+
left+", scrollbars = "+scrollbars;
if(url=="url"){
var newurl = $(".urladdress").val()+escapeStr($(".urlparam"+number).val());
var newwindow = window.open(newurl, name, strFeatures);
}else{
var newwindow = window.open(url, name, strFeatures);
}
if (window.focus) { newwindow.focus(); }
}
var features = "toolbar=no, status=yes, menubar=no, location=no,resizable=yes";
var left = Number((screen.availWidth/2) - (width/2));
var top = Number((screen.availHeight/2) - (height/2));
var strFeatures = features + ", width = "+width+", height = "+height+", top = "+top+", left = "+
left+", scrollbars = "+scrollbars;
if(url=="url"){
var newurl = $(".urladdress").val()+escapeStr($(".urlparam"+number).val());
var newwindow = window.open(newurl, name, strFeatures);
}else{
var newwindow = window.open(url, name, strFeatures);
}
if (window.focus) { newwindow.focus(); }
}
function escapeStr(str) {
escape(str);
//'%' must be first replace
//%,+,',/,",?,#,^,\,&,!,~,$,*,@,_
str = str.replace(/\%/g,'%25')
.replace(/\+/g, '%2B')
.replace(/\'/g, '%27')
.replace(/\//g, '%2F')
.replace(/\"/g,'%22')
.replace(/\?/g,'%3F')
.replace(/\#/g,'%23')
.replace(/\^/g,'%5E')
.replace(/\\/g,'%5C')
.replace(/\&/g,'%26')
.replace(/\!/g,'%21')
.replace(/\~/g,'%7E')
.replace(/\$/g,'%24')
.replace(/\*/g,'%2A')
.replace(/\@/g,'%40')
.replace(/\_/g,'%5F');
return str;
}
escape(str);
//'%' must be first replace
//%,+,',/,",?,#,^,\,&,!,~,$,*,@,_
str = str.replace(/\%/g,'%25')
.replace(/\+/g, '%2B')
.replace(/\'/g, '%27')
.replace(/\//g, '%2F')
.replace(/\"/g,'%22')
.replace(/\?/g,'%3F')
.replace(/\#/g,'%23')
.replace(/\^/g,'%5E')
.replace(/\\/g,'%5C')
.replace(/\&/g,'%26')
.replace(/\!/g,'%21')
.replace(/\~/g,'%7E')
.replace(/\$/g,'%24')
.replace(/\*/g,'%2A')
.replace(/\@/g,'%40')
.replace(/\_/g,'%5F');
return str;
}