父窗口子窗口问题搜集

根据iframe里页面的内容来控制父窗口iframe高度

在包含的IFrame页面里:
<TABLE WIDTH=75% BORDER=1 CELLSPACING=1 CELLPADDING=1>
<TR>
 <TD>
<iframe id=zz src="a.asp" height=100% scrolling=no></iframe></TD>
</TR>
</TABLE>


在a.asp里加上这些代码:
<SCRIPT LANGUAGE=javascript>
<!--
window.οnlοad=function(){
var ptd = window.parent.document.getElementById("zz");
ptd.style.height = document.body.scrollHeight;
}
//-->
</SCRIPT>

 

 

IFRAME内控制父窗口的另一个IFRAME

parent.window.frames['aaa'].document.getElementById("bbb").innerHTML="11111";

 Javascript 中父窗口与子窗口的交互

Javascript:

  1. <script type= "text/javascript">
  2. <!--
  3. function openWindow ( )
  4. {
  5.     newWindow = window. open ( '', 'newWindow', 'height=300,width=300,scrollbars=auto' );  
  6.     if (newWindow != null )
  7.     {
  8.        var windowHTML= "<html><head><title>preview</title></head>";
  9.        windowHTML += "<body><h1 align='center'>";
  10.        windowHTML += "这是子窗口!</h1><hr><div align='center'><form action='#' method='get'>";
  11.        windowHTML += "<input type='button' value='将父窗口的背景设为红色' οnclick='window.opener.document.bgColor=/"red/";' /><br>";
  12.        windowHTML += "<br ><input type='button' value='关闭' οnclick='self.close();' />";
  13.        windowHTML += "</form></div></body></html>";
  14.  
  15.        newWindow. document. write (windowHTML );
  16.        newWindow. focus ( );
  17.     }
  18. }
  19. //-->
  20. </script>

HTML:

  1. <input value='打开子窗口' onclick="openWindow();" type="button">
  2.  
  3. <input type="button" value="将子窗口的背景设为蓝色" onclick="if (window.newWindow){newWindow.document.bgColor='blue';newWindow.focus();}" />
  4.  

父窗口调用子窗口

 

父窗口与子窗口的数据传递问题

我权且把原始窗口叫父窗口,把从该窗口打开或弹出的窗口或对话框叫子窗口.当然打开子窗口可用window.open()或window.showModalDialog()[与window.showModelessDialog()类似].若想将父窗口的数据传递到子窗口可用URL后带请求字符串即"?id1=qurey1&id2=query2",在子窗口中用window.location.search来获取该请求字符串.再利用字符串分割便可获得数据.

下面通过例子来说一下,子窗口向父窗口传递数据.首先是使用window.open()方法打开的窗口.
主窗口中主要是

<scrīpt type="text/Javascrīpt">
<!--
function MM_openSubWin(theURL,winName,features)
{
   window.open(theURL,winName,features);
}
//-->
</scrīpt>

<form name="form1" id="form1">
<table width=300" border="0" align="center" cellpadding="0" cellspacing="0" align="center">
<tr>
 <td width="100">测试输入框</td>
 <td ><input name="to_mobile" type="text" id="to_mobile" value="" size="20" maxlength="11"> </td>
</tr>
<tr>
<td height="20" colspan="2">
<a href="#" ōnClick="MM_openSubWin('subwin.htm','测试子窗口1','width=450,height=300')"><font color="#FF6600">测试子窗口1</font></a>
</td>
 </tr>
 </table>  
 </form>

这里主要有个window.open().它很简单,有三个主要参数,第一个是打开子窗口的URL;第二个是打开子窗口的名字,可选;第三个是设置大小,工具条等,可选.

子窗口中代码主要是

<scrīpt type="text/Javascrīpt">
<!--
function ConfirmSelection_onclick()
{
  var strCallNumbers = new String();
  strCallNumbers = document.form2.mobile.value;
  window.parent.opener.document.form1.to_mobile.value = strCallNumbers;
}
//-->
</scrīpt>

<table width="300" height="26" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
 <td width="100">测试数据输入</td>
 <td >  <input name="mobile" type="text" id="mobile" value="" size="20" maxlength="11">
 </td>
</tr>
<tr> 
 <td height="12" colspan="3" bgcolor="#FFFFFF">
  <input type="submit" value="确定1" id="ConfirmSelection" name="ConfirmSelection" ōnclick="ConfirmSelection_onclick();window.close();">
  </td>
</tr>
 
</table>
</form>

这里主要是window.parent.opener,parent获取对象层次中的父窗口;opener设置或获取创建当前窗口的窗口的引用.使用它就可以对父窗口进行传值.

第二种方法是使用弹出对话框来实现.父窗口的代码主要有:

<scrīpt type="text/Javascrīpt">
<!--
function MM_showSubWin(theURL,varName,features)
{
  //window.showModalDialog(theURL,varName,features);
  window.showModelessDialog(theURL,varName,features);
}
//-->
</scrīpt>

<form name="form1" id="form1">
<table width=300" border="0" align="center" cellpadding="0" cellspacing="0" align="center">
<tr>
 <td width="100">测试输入框</td>
 <td ><input name="to_mobile" type="text" id="to_mobile" value="" size="20" maxlength="11"> </td>
</tr>
<tr>
 <td height="20" colspan="2">
  <a href="#" ōnClick="MM_showSubWin('subwin.htm',window,'')"><font color="#FF6600">测试子窗口2</font></a>
 </td>
</tr>
</table>  
</form>

这里主要涉及window.showModalDialog()和window.showModelessDialog().二者的功能类似,都是打开指定的对话框,主要区别是:
  showModalDialog:被打开后就会始终保持输入焦点。除非对话框被关闭,否则用户无法切换到主窗口。类似alert的运行效果。
 showModelessDialog:被打开后,用户可以随机切换输入焦点。对主窗口没有任何影响.
注意为了省事,传递变量名时直接将"window"传递过去,即showModalDialog("URL",window,"")

而子窗口对话框代码主要是

<scrīpt type="text/Javascrīpt">
<!--
function ConfirmSelection_onclick2()
{
  var strCallNumbers = new String();
  strCallNumbers = document.form2.mobile.value;
  window.dialogArguments.document.form1.to_mobile.value = strCallNumbers;
}
//-->
</scrīpt>

<table width="300" height="26" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
 <td width="100">测试数据输入</td>
 <td >  <input name="mobile" type="text" id="mobile" value="" size="20" maxlength="11">
 </td>
</tr>
<tr> 
 <td height="12" colspan="3" bgcolor="#FFFFFF">
  <input type="submit" value="确定2" id="ConfirmSelection2" name="ConfirmSelection2" ōnclick="ConfirmSelection_onclick2();window.close();">  </td>
</tr>
 
</table>
</form>

你如果不想要对话框链接时不弹出新窗口就在<head />中添加<base target="_self">代码.这里传递数据用到了window.dialogArguments,它用来设置或获取传递给模式对话框窗口的变量或变量数组.


iframe_ID.iframe_document_object.object_attribute = attribute_value
例子
onClick="iframe_text.myH1.innerText='http://www.02dz.cn';"
子窗口调用父窗口
parent.parent_document_object.object_attribute = attribute_value
例子
οnclick="parent.myH1.innerText='http://www.02dz.cn';"

上面在IE下没有问题,但在firefox下不正常。在firefox下,应该是
父窗口调用子窗口
window.frames["iframe_ID"].document.getElementById("iframe_document_object"­).object_attribute = attribute_value
例子
window.frames["iframe_text"].document.getElementById("myH1").innerHTML= "http://www.02dz.cn";
子窗口调用父窗口
parent.document.getElementById("parent_document_object").object_attribute = attribute_value
例子
parent.document.getElementById("myH1").innerHTML = "http://www.02dz.cn";

 

 子窗口更改父窗口元素

父窗口

<SCRIPT language=javascript>
  function PopupCatSel()
  {
   var selProdWnd=window.open("/member/tradeinfo/select_sort.asp","new","resizable=yes,width=580,height=320,top=0,scrollbars=yes");
   if(selProdWnd.opener== null)
   selProdWnd.opener = self;
   return (false);
  }
</SCRIPT>

子窗口

<SCRIPT LANGUAGE="JavaScript">
function retForm(sortid,type3id,sortname,typeid,typename1,type3name)

 if (!opener) return true;
 opener.document.forms[0].sortid.value = sortid;
 if (!opener) return true;
 opener.document.forms[0].typeid.value = typeid;
 if (!opener) return true;
 opener.document.forms[0].type3id.value = type3id;
 if(opener.document.forms[0].cat_sel_name)
 opener.document.forms[0].cat_sel_name.value = sortname+ " >> "+ typename1+ " >> "+ type3name;
  opener.document.forms[0].cat_sel_name1.value = sortname+ " >> "+ typename1+ " >> "+ type3name;
 self.close();
 return false;
}
</SCRIPT>

 

关于window.opener的用法

window.opener 的用法
    window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了b.htm,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id为“name”的textbox中,就可以写为:
    
    window.opener.document.getElementById("name").value = "输入的数据";
    
    
    对于javascript中的window.opener没有很好的理解。
    为什么框架中不能使用,弹出窗口的父窗口不能在框架里面的某个页面呢?那怎样通过弹出窗口操作框架中的父窗口呢?
    
    opener.parent.frames['frameName'].document.all.input1.value 试试这个:)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值