javascript 中设置window.location.href跳转无效问题解决办法

发布时间:2020-10-19 12:56:08 作者:lqh
来源:脚本之家 阅读:703

javascript 中设置window.location.href跳转无效问题解决办法

问题情况

JS中设置window.location.href跳转无效

代码如下:

<script type="text/javascript"> 
  function checkUser() 
{  
   if(2!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;  
   } 
} 
 </script>  
 
<div class="extra"> 
     <a class="ui blue right floated primary button" onclick="checkUser()" href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime }" rel="external nofollow" rel="external nofollow" >确认预订</a> 
      </div> 

原因是 a标签的href跳转会执行在window.location.href设置的跳转之前:

如果是表单form的话  也会先执行form提交。

提交之后 就已经不在当前页面了。所以 window.location.href无效。

解决方法一

在js函数中加上

window.event.returnValue=false

这个属性放到提交表单中的onclick事件中在这次点击事件不会提交表单,如果放到超链接中则在这次点击事件不执行超链接href属性。

改成如下代码后window.location.href成功跳转:

<script type="text/javascript"> 
  function checkUser() 
{  
   if(2!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;  
   window.event.returnValue=false; 
   } 
} 
 </script>  
 
<div class="extra"> 
     <a class="ui blue right floated primary button" onclick="checkUser()" href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime }" rel="external nofollow" rel="external nofollow" >确认预订</a> 
      </div> 

解决方法二

点击事件中  onclick="checkUser()"  变成 onclick="return checkUser();"

并且在 checkUser中 return  false;这样的话 a标签的href也不会执行。 这样就能window.location.href顺利跳转。

代码如下:

<script type="text/javascript"> 
  
  function checkUser() 
{  
   if(<%=flag%>!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; 
   return false; 
   } 
} 
 </script> 
 
 <div class="extra"> 
     <a class="ui blue right floated primary button" onclick="return checkUser();"  
 
href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime  
 
}">确认预订</a> 
      </div> 

解决方法三

如果是form体提交的话还可以把summit改成button调用js提交,这样window.location.href也会在js提交summit之前执行成功跳转。

如下:

function checkUser() 
{  
   if(<%=flag%>!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; 
   return false; 
   } 
 document.getElementById("form").submit(); 
} 
 
 
  <form action="addRoom" method="post"  name="from" id="form"> 
      <table align="center" border="1" class="commTable"> 
        <tr> 
          <td class="right"><span 
            >房号:</span></td> 
          <td><input type="text" name="roomNum" size="25" 
            id="roomNum" /></td> 
        </tr> 
        <tr> 
          <td colspan="2" align="center"><button  value="添加" 
            onclick="checkUser()" /></td> 
        </tr> 
      </table> 
    </form> 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

推荐阅读:
  1. JavaScript中location.href和window.open的区别是什么
  2. 利用JavaScript怎么模拟一个锚点跳转功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript 跳转无效 avascript

上一篇:微信小程序 bindtap 传参的实例代码

下一篇:javascript实现摄像头拍照预览

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》