頁面A,其內部有個iframe,部分 html代碼如下:
[html]
<div><iframe name="ifm" width="100%" height="829" id="ifmApp" src="B.asp?co=1&tp=5&ut=1&id=&gd=11&gm=1&ar=2&sv=0" frameBorder="0" marginWidth="0" marginHeight="0" scrolling="no"></div>
A頁面中有段js指令碼如下:
www.2cto.com
[javascript]
var System={
CloseFrame:function () {
try{
var frames=document.getElementById("ifmApp");
var AppHelper= frames.contentWindow.AppHelper;
if(AppHelper!=null && typeof(AppHelper)=="object"&&typeof(AppHelper.CloseQuery)=="function"){
if(!AppHelper.CloseQuery())return ;
}
}catch(e){
}
System.Close();
},
Close:function () {
document.body.removeChild(ifrm);
}
};
iframe src所對應的B.asp頁面,其中有js函數:
[javascript]
var Apphelper={
CloseQuery:function(){
if(val="1")return true;
return false;
},
Close:function(){
if(parent!=null&&typeof(parent)=="object"){
var System=parent.System;
if(System!=null && typeof(System)=="object"){
if(System.Close!=null && typeof(System.Close)=="function"){
System.Close();
return;
}
}
}
}
};
父頁面A,通過調用自己的System.CloseFrame來調用ifrme中的函數Apphelper.CloseQuery
Ifame中的B頁面,通過調用自己的Apphelper.Close函數來調用父視窗中的System.Close
當然,實現這樣的簡單調用有個前提,就是A和B需要同域。
摘自 心靈淨土