場景:界面NewsView.aspx嵌套在NewsCenterFrame.aspx中的Iframe中
<iframe src="NewsView.aspx" runat="server" width="749" height="93%" frameborder="0" scrolling="yes"name="frame_right" id="frame_right"></iframe>
如何從外部新聞鏈接中打開NewsView.aspx界面而且確保它打開后還是嵌套在NewsCenterFrame.aspx,如果鏈接本身就是在NewsCenterFrame.aspx中那當然萬事大吉,直接用target="frame_right"就行了,可如果不是呢,打開的結果則只是NewsView.aspx。
我解決這個問題的思路是在NewsView.aspx和NewsCenterFrame.aspx上面解決,不去改動鏈接。
NewsView.aspx:
$(document).ready(function () { if (self.frameElement == null) { //判斷當前界面是否在iframe中 var thisSLoc = self.location.href; //獲取當前界面的url全路徑 var tmpUPage = thisSLoc.split("/"); var thisUPage = tmpUPage[tmpUPage.length - 1];//獲取當前頁面的頁面名+所有參數 window.location.href = "NewsCenterFrame.aspx?" + thisUPage;//打開要嵌入的父頁面 } else { $("#form1").css("display","inline");//讓頁面不顯示,因為是先執行后台代碼的,為了讓頁面再跳轉的過程中NewsView.aspx的內容不顯示,先把Form1隱藏掉,如果判斷它已經在Iframe中則將它顯示出來 } })
NewsCenterFrame.aspx:
$(document).ready(function () { var thisSLoc = self.location.href; var firstindex = thisSLoc.indexOf("?"); var frameurl = thisSLoc.substring(firstindex + 1, thisSLoc.length); $("#frame_right").attr("src", frameurl); })
