现在的位置: 首页 > 综合 > 正文

window.parent与window.opener的区…

2018年05月10日 ⁄ 综合 ⁄ 共 2566字 ⁄ 字号 评论关闭

1、window.parent 是iframe页面调用父页面对象

举例: a.html

<html>
<head><title>A</title></head>
<body>
<form name="form1" id="form1">
<input type="text" name="username"id="username"/>
</form>
<iframe src="b.html"width=100%></iframe>
</body>
</html>

如果我们需要在b.html中要对a.html中的username文本框赋值(就如很多上传功能,上传功能页在ifrmae中,上传成功后把上传后的路径放入父页面的文本框中),我们应该在b.html中写:

<scripttype="text/javascript">
var _parentWin = window.parent;
_parentWin.form1.username.value = "xxxx";

2、window.opener 是 window.open 打开的子页面调用父页面对象

opener:对打开当前窗口的window对象的引用,如果当前窗口被用户打开,则它的值为null。

self代表自身窗口,opener代表打开自身的那个窗口,比如窗口a.html打开窗口b.html。如果靠window.open方法,则对于窗口b.html,self代表b.html自己,而opener代表窗口a.html。

举例:a.html

<input type="text" name="username"id="username"/>
<aonclick="window.open(this.href,'','resizable=yes,width=800,height=600,status');return false"href="b.html">B</a>

如果需要在b.html中对a.html中的表单元素赋值,我们应该在b.html中这么写

<ahref="javascript:try{window.opener.document.getElementByIdx_x('username').contentWindow.
frames[0].document.getElementsByTagName_r('body')[0].innerHTML+='xxx'}catch(e){};window.close();">插入</a>

在后面用window.close关闭b.html

"window.location.href"、"location.href" 当前页面跳转
"parent.location.href":上一级父页面跳转
"top.location.href":顶级页面跳转

连接的时候target的用法了:
_blank:重新打开一个窗口
_parent:父窗口执行重定向
_self:自身页面重定向
_top:第一个父窗口重定向

内嵌iframe:
window、self、window.self 当前页面对象。
window.parent  是iframe页面调用父页面对象。
window.top       顶层页面对象。
oepn:
window.open   返回的是子页面对象
window.opener 在子页面中使用父类对象

contentWindow属性是指指定的frame或者iframe所在的window对象

在IE中iframe或者frame的contentWindow属性可以省略,但在Firefox中如果要对iframe对象进行编辑则

必须指定contentWindow属性。

function EnableEdit()
{
      var editor;
      editor = document.getElementById("HtmlEdit").contentWindow;
   // 针对IE浏览器, make it editable
      editor.document.designMode = 'On';
      editor.document.contentEditable = true;
   // For compatible with FireFox, it should open and write something to make it work
editor.document.open();
editor.document.writeln('<html><head>');
editor.document.writeln('<style>body {background: white;font-size:9pt;margin: 2px; padding: 0px;}</style>');
editor.document.writeln('</head><body></body></html>');
editor.document.close();

}

<iframe   ID="HtmlEdit" MARGINHEIGHT="1" MARGINWIDTH="1" width="100%" height="312">
</iframe>

<html>
<body>
<script>
var ifr = document.createElement("iframe");
document.body.appendChild(ifr);
var ifrdoc = ifr.contentWindow.document;
var s = fixingHTB.innerHTML;   //进入可编辑模式前存好
ifrdoc.designMode = "on";     //文档进入可编辑模式
ifrdoc.open();                        //打开流
ifrdoc.write(s);
ifrdoc.close();                        //关闭流
ifrdoc.designMode ="off";     //文档进入非可编辑模式
</script>
</body>
 
 
var frames=window.top.window.document.getElementById("rightframe");
   frames.contentWindow.TshowMsg(msg,'');

抱歉!评论已关闭.