javascript 中設(shè)置window.location.href跳轉(zhuǎn)無效問題解決辦法

創(chuàng)新互聯(lián)主營興安網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App定制開發(fā),興安h5成都微信小程序搭建,興安網(wǎng)站營銷推廣歡迎興安等地區(qū)企業(yè)咨詢
問題情況
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)會執(zhí)行在window.location.href設(shè)置的跳轉(zhuǎn)之前:
如果是表單form的話 也會先執(zhí)行form提交。
提交之后 就已經(jīng)不在當(dāng)前頁面了。所以 window.location.href無效。
解決方法一
在js函數(shù)中加上
window.event.returnValue=false
這個(gè)屬性放到提交表單中的onclick事件中在這次點(diǎn)擊事件不會提交表單,如果放到超鏈接中則在這次點(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也不會執(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也會在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
>房號:</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>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
本文標(biāo)題:javascript中設(shè)置window.location.href跳轉(zhuǎn)無效問題解決辦法
瀏覽路徑:http://chinadenli.net/article32/jiispc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)