子窗口和父窗口交互

 

None.gif 最近项目开发中需要子窗口和父窗口交互的内容,基本上无非就是把子窗口的信息传递给父窗口,并且关闭自己等等,或者是父窗口把自己的信息传递给子窗口等等。
None.gif
None.gif1。父窗口传递信息给子窗口
None.gif
None.gif看代码实例:
ExpandedBlockStart.gifContractedBlock.gif
< script  language =javascript > dot.gif
InBlock.gif
InBlock.gif
function outPut()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
//获取父窗口的文本信息赋值给text
InBlock.gif
 var text = document.abc.text.value;
InBlock.gif 
//打开子窗口,并且把操作句柄赋值给win变量,以下所有操作都是针对win对象的
InBlock.gif
 var win = window.open("","mywin""menubar=no,width=400,height=100,resizeable=yes");
InBlock.gif 
//输出基本信息
InBlock.gif
 win.document.writeln("输出结果");
InBlock.gif win.document.writeln(
"你的信息是:

");
InBlock.gif 
//输出从父窗口获取的信息
InBlock.gif
 win.document.writeln(text);
InBlock.gif win.document.close();
InBlock.gif win.focus();
ExpandedBlockEnd.gif}

None.gif
script >
None.gif
None.gif
< form  name =abc  method =post >
None.gif
< input  type =text  name =text  size =50 >
None.gif//调用上面的函数
None.gif
< input  type =button  value =提交  onClick ="outPut()" >
None.gif
None.gif
form >
None.gif
None.gif
None.gif2。子窗口传递参数给父窗口
None.gif
None.gif我们对上面的代码进行改造:
None.gif
ExpandedBlockStart.gifContractedBlock.gif
< script  language =javascript > dot.gif
InBlock.gif
InBlock.gif
function outPut()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
var text = document.abc.text.value;
InBlock.gif 
var win = window.open("","mywin""menubar=no,width=400,height=100,resizeable=yes");
InBlock.gif win.document.writeln(
"输出结果");
InBlock.gif win.document.writeln(
"你的信息是:

");
InBlock.gif win.document.writeln(text);
InBlock.gif win.document.writeln(
"");
InBlock.gif
InBlock.gif 
//对子窗口本身操作,使用self对象,对父窗口操作使用opener对象,这是关键
InBlock.gif
 //把子窗口中表单的值回传给父窗口,取代父窗口表单以前的值,然后关闭子窗口
InBlock.gif
 win.document.writeln("");
InBlock.gif 
//可以控制关闭父窗口
InBlock.gif
 win.document.writeln("");
InBlock.gif 
//刷新父窗口
InBlock.gif
 win.document.writeln("");
InBlock.gif
InBlock.gif win.document.close();
InBlock.gif win.focus();
ExpandedBlockEnd.gif}

None.gif
script >
None.gif
None.gif
< form  name =abc  method =post >
None.gif
< input  type =text  name =text  size =50 >
None.gif
< input  type =button  value =提交  onClick ="outPut()" >
None.gif
None.gif
form >
None.gif
None.gif
None.gif3。不是同页面的子窗口和父窗口交互
None.gif
None.gif假设我们涉及到外部程序,比如php、asp等等,那么我们处理的可能是两个页面,比如,上传功能,我们就是需要打开一个新页面,然后再把新页面中的值传递给父页面。
None.gif
None.gif局部代码实例:
None.gif
None.gif
< input  type ="input"  value =""  name ="input_tag"  id  = "input_tag"  onKeyUp ="clearPreTagStyle()"  size ="40" >
None.gif
< input  type ="hidden"  value ="0"  name ="tagid"  id ="tagid" >
None.gif
< input  type ="button"  value ="标签"  onclick ="popUpWindow('tag.php?tag='+escape(document.tryst_form.input_tag.value))" >
None.gif
None.gif以上是父窗口的部分代码,里面的popUpWindow是封装好的window.open函数,所以理解面面的tag.php是另外一个页面就可以,我们需要把当前表单中的值提交给tag.php页面去处理。
None.gif
None.gif
None.giftag.php部分代码:
None.gif
None.gif查询标签结果:
None.gif
< href ="#"  name ="tag_1" > 生活 a >< href ="#"  onclick ="opener.document.tryst_form.input_tag.value = document.tag_1.innerHTML" > 加入该标签 a >
None.gif
None.gif
< href ="#"  name ="tag_2" > 生活秀 a >< href ="#"  onclick ="opener.document.tryst_form.input_tag.value = document.tag_2.innerHTML" > 加入该标签 a >
None.gif
None.gif这个就是我们的子窗口,我们要把tag_1和tag_2返回到子窗口中,虽然他们不是同一个页面。这里有个知识点,就是我们如何获取连接中的值,我们使用innerHTML属性:document.tag_2.innerHTML 这个就是我们获取了tag_2的值“生活秀”,我们也能使用其他方法获取,比如:document.all.tag_2.innerHTML,或者this.innerHTML就是指本身的链接的值。
None.gif
None.gif访问父窗口也是使用opener对象来处理:opener.document.tryst_form.input_tag.value,就能够改变父窗口的值。
None.gif

None.gif 1 . 在Asp.net实用技巧( 1 ) 中提到了如何刷新父页面,那么如果要刷新父页面的父页面的父页面了?那就是刷新祖先页面RefreshAncestorPage。
None.gif
ContractedBlock.gifExpandedBlockStart.gifRefreshAncestorPage
RefreshAncestorPage #region RefreshAncestorPage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// 
InBlock.gif        
/// 刷新指定的祖先页面,注意是"祖先页面"    
ExpandedSubBlockEnd.gif        
/// 
        

InBlock.gif        public static void RefreshAncestorPage(HttpResponse Response ,string targetPageTitle ,bool isCloseCurPage)//targetPageTitle 目标页面的title
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{            
InBlock.gif            StringBuilder scriptString 
= new StringBuilder();
InBlock.gif            scriptString.Append(
"");
InBlock.gif            scriptString.Append(
"var p = window ;");
InBlock.gif            scriptString.Append(
string.Format("while(p.document.title != '{0}')" ,targetPageTitle));
InBlock.gif            scriptString.Append(
"{");            
InBlock.gif            scriptString.Append(
"p = p.opener ;");
InBlock.gif            scriptString.Append(
"}");            
InBlock.gif            scriptString.Append(
"p.focus();");
InBlock.gif            scriptString.Append(
"p.refresh();");
InBlock.gif
InBlock.gif            
if (isCloseCurPage )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                scriptString.Append( 
" window.focus();" );
InBlock.gif                scriptString.Append( 
" window.opener=null;" );
InBlock.gif                scriptString.Append( 
" window.close(); " );
ExpandedSubBlockEnd.gif            }
            
InBlock.gif
InBlock.gif            scriptString.Append(
""+"script>");
InBlock.gif
InBlock.gif            Response.Write(scriptString.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//*
InBlock.gif         需要在Father页面的html中添加如下脚本(在Header中):
InBlock.gif         
InBlock.gif        function refresh()
InBlock.gif        {
InBlock.gif            this.location = this.location;
InBlock.gif        }
InBlock.gif        
ExpandedSubBlockEnd.gif         
*/
        
ExpandedBlockEnd.gif        
#endregion

None.gif
2 .如何刷新祖先页面中的某个frame中的page了?
None.gif
ContractedBlock.gifExpandedBlockStart.gifRefreshFrameInAncestorPage
RefreshFrameInAncestorPage #region RefreshFrameInAncestorPage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// 
InBlock.gif        
/// 刷新指定的祖先页面中的某个框架的内部页面
ExpandedSubBlockEnd.gif        
/// 
        

InBlock.gif        public static void RefreshFrameInAncestorPage(HttpResponse Response ,string ancestorTitle ,string frameName ,string targetUrl ,bool isCloseCurPage)//targetPageTitle 目标页面的title
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{            
InBlock.gif            StringBuilder scriptString 
= new StringBuilder();
InBlock.gif            scriptString.Append(
"");
InBlock.gif            scriptString.Append(
"var p = window ;");
InBlock.gif            scriptString.Append(
string.Format("while(p.document.title != '{0}')" ,ancestorTitle));
InBlock.gif            scriptString.Append(
"{");            
InBlock.gif            scriptString.Append(
"p = p.opener ;");
InBlock.gif            scriptString.Append(
"}");            
InBlock.gif            scriptString.Append(
"p.focus();");
InBlock.gif            scriptString.Append(
string.Format("p.{0}.location = '{1}';" ,frameName, targetUrl));
InBlock.gif
InBlock.gif            
if (isCloseCurPage )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                scriptString.Append( 
" window.focus();" );
InBlock.gif                scriptString.Append( 
" window.opener=null;" );
InBlock.gif                scriptString.Append( 
" window.close(); " );
ExpandedSubBlockEnd.gif            }
            
InBlock.gif
InBlock.gif            scriptString.Append(
""+"script>");
InBlock.gif
InBlock.gif            Response.Write(scriptString.ToString());
ExpandedSubBlockEnd.gif        }
        
ExpandedBlockEnd.gif        
#endregion

None.gif
3 .如何刷新本页面中的其它框架了?
None.gif
ContractedBlock.gifExpandedBlockStart.gifRefreshTargetFrameInSamePage
RefreshTargetFrameInSamePage #region RefreshTargetFrameInSamePage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// 
InBlock.gif        
/// 从某一框架刷新同一页面中的任意一框架(包括自己所处的框架)
ExpandedSubBlockEnd.gif        
/// 
        

InBlock.gif        public static void RefreshTargetFrameInSamePage(HttpResponse Response ,string frameName ,string targetUrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{                
InBlock.gif            
string scripStr = string.Format(" window.parent.{0}.location= '" ,frameName) +targetUrl + "'";
InBlock.gif            scripStr 
+= ""+"script>" ;            
InBlock.gif            Response.Write(scripStr) ;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
 
None.gif
None.gif
4 .如何调用祖先页面的脚本?
None.gif
ContractedBlock.gifExpandedBlockStart.gifCallAncestorScriptMethod
CallAncestorScriptMethod #region CallAncestorScriptMethod
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// 
InBlock.gif        
/// 调用祖先页面中的某个框架内部page的脚本 ,如果是调用祖先页面的脚本,targetFrameName传入null
ExpandedSubBlockEnd.gif        
/// 
        

InBlock.gif        public static void CallAncestorScriptMethod(HttpResponse Response ,string targetPageTitle ,string targetFrameName ,string methodName ,string[] paraStrs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder scriptString 
= new StringBuilder();
InBlock.gif            scriptString.Append(
"");
InBlock.gif            scriptString.Append(
"var p = window ;");
InBlock.gif            scriptString.Append(
string.Format("while(p.document.title != '{0}')" ,targetPageTitle));
InBlock.gif            scriptString.Append(
"{");            
InBlock.gif            scriptString.Append(
"p = p.opener ;");
InBlock.gif            scriptString.Append(
"}");    
InBlock.gif            
if(targetFrameName != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(paraStrs == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    scriptString.Append(
string.Format("p.frames['{0}'].{1}() ;" ,targetFrameName ,methodName ));
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string rParaStr = string.Format("'{0}'" ,paraStrs[0]) ;
InBlock.gif                    
for(int i=1 ;i<paraStrs.Length ;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        rParaStr 
+= string.Format(", '{0}'" ,paraStrs[i]) ;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    scriptString.Append(
string.Format("p.frames['{0}'].{1}({2}) ;" ,targetFrameName ,methodName ,rParaStr));    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(paraStrs == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    scriptString.Append(
string.Format("p.{0}() ;" ,methodName ));    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string rParaStr = string.Format("'{0}'" ,paraStrs[0]) ;
InBlock.gif                    
for(int i=1 ;i<paraStrs.Length ;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        rParaStr 
+= string.Format(", '{0}'" ,paraStrs[i]) ;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    scriptString.Append(
string.Format("p.{0}({1}) ;" ,methodName ,rParaStr));        
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }
            
InBlock.gif
InBlock.gif            scriptString.Append(
""+"script>");
InBlock.gif            Response.Write(scriptString.ToString());
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
None.gif
None.gif
5 .如何调用本页面中其它框架page的脚本?
None.gif
ContractedBlock.gifExpandedBlockStart.gifCallTargetFrameScriptMethodInSamePage
CallTargetFrameScriptMethodInSamePage #region CallTargetFrameScriptMethodInSamePage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// 
InBlock.gif        
/// 调用本页面中其它框架内部page的脚本 ,
ExpandedSubBlockEnd.gif        
/// 
        

InBlock.gif        public static void CallTargetFrameScriptMethodInSamePage(HttpResponse Response ,string targetFrameName ,string methodName ,string[] paraStrs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder scriptString 
= new StringBuilder();
InBlock.gif            scriptString.Append(
"");
InBlock.gif
InBlock.gif            
if(paraStrs == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                scriptString.Append(
string.Format("window.parent.{0}.{1}() ; ;" ,targetFrameName ,methodName));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string rParaStr = string.Format("'{0}'" ,paraStrs[0]) ;
InBlock.gif                
for(int i=1 ;i<paraStrs.Length ;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    rParaStr 
+= string.Format(", '{0}'" ,paraStrs[i]) ;
ExpandedSubBlockEnd.gif                }

InBlock.gif                scriptString.Append(
string.Format("window.parent.{0}.{1}({2}) ; ;" ,targetFrameName ,methodName ,rParaStr));    
ExpandedSubBlockEnd.gif            }

InBlock.gif                
InBlock.gif            scriptString.Append(
""+"script>");
InBlock.gif            Response.Write(scriptString.ToString());
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

你可能感兴趣的:(子窗口和父窗口交互)