最近在做項目的時候,使用window.open()打開一個子視窗,父視窗卻意外的關閉了。
當點選 “我的資料” 打開我的資料子視窗的時候,父視窗莫名其妙的關閉了,退到了登入界面:
這個問題在IE9下是正常的, 可是在火狐,chrome,opera等非IE核心的浏覽器中都會出現。
代碼是這麼寫的:
<li>
<a href="#" target="_blank" rel="external nofollow" οnclick="accountInfo('<%=basePath%>account.do?method=accountInfo&from=myaccount&type=view&accountId=<%=SessionUtil.getuser().getAccountId()%>')">
<i class="icon-user"></i>
我的資料
</a>
</li>
經過與其他項目的對比排查,找到了原因:
在<a>标簽中使用了'#'代替了實際的位址,而使用onclick()函數觸發事件,原因就在這裡,隻要把上面的代碼改為:
<li>
<a href="accountInfo('<%=basePath%>account.do?method=accountInfo&from=myaccount&type=view&accountId=<%=SessionUtil.getuser().getAccountId()%>')" target="_blank" rel="external nofollow" >
<i class="icon-user"></i>
我的資料
</a>
</li>
将href指向js函數即可,不使用onclick觸發事件,這樣再打開子視窗的時候,父視窗就沒什麼變化了。