javascript 中設(shè)置window.location.href跳轉(zhuǎn)無(wú)效問(wèn)題解決辦法
javascript 中設(shè)置window.location.href跳轉(zhuǎn)無(wú)效問(wèn)題解決辦法
問(wèn)題情況
JS中設(shè)置window.location.href跳轉(zhuǎn)無(wú)效
代碼如下:
<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à) 也會(huì)先執(zhí)行form提交。
提交之后 就已經(jīng)不在當(dāng)前頁(yè)面了。所以 window.location.href無(wú)效。
解決方法一
在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;這樣的話(huà) 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體提交的話(huà)還可以把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 style="font-weight: blod;">房號(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ì)本站的支持!
相關(guān)文章
Javascript 獲取字符串字節(jié)數(shù)的多種方法
Javascript 字符串字節(jié)數(shù)獲取功能多種方法2009-06-06JS中typeof與instanceof之間的區(qū)別總結(jié)
本文是對(duì)JS中typeof與instanceof之間的區(qū)別進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11微信小程序登錄時(shí)如何獲取input框中的內(nèi)容
這篇文章主要介紹了微信小程序登錄時(shí)如何獲取input框中的內(nèi)容,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Bootstrap table表格初始化表格數(shù)據(jù)的方法
這篇文章主要介紹了Bootstrap-table表格初始化表格數(shù)據(jù)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07微信小程序?qū)崿F(xiàn)錄音時(shí)的麥克風(fēng)動(dòng)畫(huà)效果實(shí)例
這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)錄音時(shí)的麥克風(fēng)動(dòng)畫(huà)效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05JavaScript常見(jiàn)的跨標(biāo)簽頁(yè)通信方式總結(jié)
跨標(biāo)簽頁(yè)通信是指在瀏覽器中的不同標(biāo)簽頁(yè)之間進(jìn)行數(shù)據(jù)傳遞和通信的過(guò)程,這篇文章為大家整理了前端常見(jiàn)的跨標(biāo)簽頁(yè)通信方式,有需要的小伙伴可以了解下2023-10-10一個(gè)多瀏覽器支持的背景變暗的div并可拖動(dòng)提示窗口功能的代碼
兼容IE、Firefox、Opera前幾天在網(wǎng)上找了許多資料,看了不少兄弟的源碼,一直找不到合適的,要不就是拖動(dòng)有問(wèn)題,要不就是不兼容Firefox,所以自已寫(xiě)了一個(gè),下面是代碼:2008-04-04