页面间(窗口间)的取值赋值及获取iframe下的window对象

①同一个窗口中,获取某个iframe的信息


<body>
  <iframe id="PAID" name="PA" src="ItemSetting.htm"></iframe>
  <iframe id="DKID" name="DK" src="ItemSetting.htm"></iframe>
</body>

var A = top.frames["PA"];//等效于document.getElementById("PAID").contentWindow

A获得的是iframe(PA)的上下文:window。

通过A可以操作该iframe的DOM对象和JS对象,例

var main = A.document.getElementById("mainDiv");//通过原生JS获取
var list = $(A.document).find("select");//通过JQ获取
var params = A.ParamList;//获取A页面中的JS对象
var values = A.getNames();//执行A页面JS中的Function

在不跨域的情况下 ,如果想通过某个iframe中的window对象获取其所在iframe DOM的话,可以用以下方法解决

//(前提是iframe必须有id)
var
id= window.name; var targetIframe = top.document.getElementById(id);

或者

window.frameElement

这是因为window对象的name取自于iframe的id或name

②通过元素获取当前window对象(document)

var but = document.getElementById("xxbut");
var doc = but.ownerDocument;

③通过window.open方式的子窗口与父窗口进行交互


var newWin = window.open(someUrl);
var parentWin = window.opener;

newWin是新打开的窗口的BOM对象
所以可以使用BOM的所有方法

常用的有
newWin.location.reload(true);//只有一个参数.true表示
newWin.focus();
newWin.close();等等

当然也可以获取到子窗口的JS对象和函数(参考①)
parentWin 是打开该子窗口的BOM对象(不一定是父窗口,因为当初打开子窗口的有可能是父窗口中的iframe)

使用parentWin.top 就可以得到父窗口的window对象了

top.location.href 顶层frame地址

self.location.href 当前frame地址

通过
top.location.href==self.location.href 
可确认当前页面是否被其他页面所装载

获取父窗口焦点(只在IE下有效)

window.opener.focus();
原文地址:https://www.cnblogs.com/TiestoRay/p/2660524.html