日期:2014-05-16  浏览次数:20306 次

JSP中input按钮跳转onclick事件要用绝对路径
JSP中利用basePath功能解决绝对路径的简化问题大家都知道的。
顺便贴个代码
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>">

今天在用Eclipse在后台测试input button onclick事件的时候。
发现IE,与FF,Chrome 等在onclik事件的路径上路径不兼容
例如本项目中代码:
<input type="button" onclick="location.href='admin/admin.do?act=goInput'" value="注册" />

如果以上写法,FF,和Chrome中会直接跳转到
http://localhost:8080/appname/admin/admin.do?act=goInput
这是正确路径。
而在IE中,则会跳转到
http://localhost:8080/appname/admin/admin/admin.do?act=goInput
这里明显错误的路径。
后来发现,onclick事件必须用绝对路径才能解决兼容问题。
所以,onclick事件里的location.href属性不能偷懒简写,必须加上<%=basePath%>
也就是如下代码:
<input type="button" onclick="location.href='<%=basePath%>admin/admin.do?act=goInput'" value="注册" />

以上代码就没有IE与FF,Chrome的兼容问题了。