首先总结location.href=“xxxx”的多种用法如下:
self.location.href="/url" 当前页面打开URL页面
location.href="/url" 当前页面打开URL页面
windows.location.href="/url" 当前页面打开URL页面,前面三个用法相同。
this.location.href="/url" 当前页面打开URL页面
parent.location.href="/url" 在父页面打开新页面
top.location.href="/url" 在顶层页面打开新页面
有些时候可能会遇到无法跳转的问题,这需要正确使用上面的跳转用法。
比如在前台有人会用target和iframe来实现提交表单数据但不跳转
<form action="" method="post" target="form_iframe">
<input type="text" id="" name="text" />
<input type="submit" id="" name="" value="提交" />
</form>
<iframe id="form_iframe" name="form_iframe" style="display:none;"></iframe>
此时如果在后台使用windows.location.href="/url"的方法你会发现页面毫无动静,其实并不是没有跳转,而是因为target指向了iframe,iframe跳转到了指定页面,又因为iframe设置了display:none被隐藏了,所以会让人误认为页面没有跳转。
此时解决办法是将windows.location.href="/url"修改为parent.location.href="/url"即可。
out.print("<script>alert('注册成功!前往登录');parent.location.href='/DepartmentStore/jsp/login.jsp';</script>");
来源:CSDN
作者:DoNg_2521
链接:https://blog.csdn.net/h2503652646/article/details/103788786