您好,登錄后才能下訂單哦!
javascript 中設(shè)置window.location.href跳轉(zhuǎn)無效問題解決辦法
問題情況
JS中設(shè)置window.location.href跳轉(zhuǎn)無效
代碼如下:
<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" >確認(rèn)預(yù)訂</a> </div>
原因是 a標(biāo)簽的href跳轉(zhuǎn)會(huì)執(zhí)行在window.location.href設(shè)置的跳轉(zhuǎn)之前:
如果是表單form的話 也會(huì)先執(zhí)行form提交。
提交之后 就已經(jīng)不在當(dāng)前頁面了。所以 window.location.href無效。
解決方法一
在js函數(shù)中加上
window.event.returnValue=false
這個(gè)屬性放到提交表單中的onclick事件中在這次點(diǎn)擊事件不會(huì)提交表單,如果放到超鏈接中則在這次點(diǎn)擊事件不執(zhí)行超鏈接href屬性。
改成如下代碼后window.location.href成功跳轉(zhuǎn):
<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" >確認(rèn)預(yù)訂</a> </div>
解決方法二
點(diǎn)擊事件中 onclick="checkUser()" 變成 onclick="return checkUser();"
并且在 checkUser中 return false;這樣的話 a標(biāo)簽的href也不會(huì)執(zhí)行。 這樣就能window.location.href順利跳轉(zhuǎn)。
代碼如下:
<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 }">確認(rèn)預(yù)訂</a> </div>
解決方法三
如果是form體提交的話還可以把summit改成button調(diào)用js提交,這樣window.location.href也會(huì)在js提交summit之前執(zhí)行成功跳轉(zhuǎn)。
如下:
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 >房號(hào):</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>
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。