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的兼容问题了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值