js操作iframe的⼀些⽅法介绍
存在跨域访问限制。
chrome:iframeElement. contentWindow
现在完成时的结构
firefox: tWindow
带胜的成语
ie6:tWindow
⽂章Iframes, onload, and document.domain中说“he iframe element object has a property called contentDocument that contains the iframe's document object, so you can u the parentWindow property to retrieve the window object.”意思就是⼀些浏览器可以通过tDocument.parentWindow获得iframe的window对象。但经过测试firefox、chrome的tDocument对象没有parentWindow属性。
(javascript)
复制代码代码如下:
function getIframeWindow(element){
return tWindow;
//return tWindow || tDocument.parentWindow;
}
存在跨域访问限制。
chrome:tDocument
firefox:tDocument
ie:tWindow.document
备注:ie没有tDocument属性。
(javascript)
复制代码代码如下:
var getIframeDocument = function(element) {
return tDocument || tWindow.document;
};
存在跨域访问限制。
⽗页⾯:window.parent
顶层页⾯:p
适⽤于所有浏览器
存在跨域访问限制。
window.frameElement(类型:HTMLElement),适⽤于所有浏览器
⾮ie浏览器都提供了onload事件。例如下⾯代码在ie中是不会有弹出框的。
(javascript)炎热夏天
复制代码代码如下:
var ifr = ateElement('iframe');
ifr.src = 'www.jb51/index.php';
alert('loaded');
鱼骨辫教程一步一步教
};
document.body.appendChild(ifr);
但是ie却⼜似乎提供了onload事件,下⾯两种⽅法都会触发onload盘发图片
特点的近义词
复制代码代码如下:
<iframe onload="alert('loaded');" src="www.jb51/index.php"></iframe>
//只有ie才⽀持为createElement传递这样的参数
复制代码代码如下:
党外人士座谈会var ifr = ateElement('<iframe onload="alert('loaded');" src="www.jb51/index.php"></iframe>'); document.body.appendChild(ifr);
由于iframe元素包含于⽗级页⾯中,因此以上⽅法均不存在跨域问题。
经济运行分析
实际上IE提供了onload事件,但必须使⽤attachEvent进⾏绑定。
复制代码代码如下:
var ifr = ateElement('iframe');
ifr.src = 'b.jb51/b.php';
if (ifr.attachEvent) {
ifr.attachEvent('onload', function(){ alert('loaded'); });
} el {
}
document.body.appendChild(ifr);
window.frames可以取到页⾯中的帧(iframe、frame等),需要注意的是取到的是window对象,⽽不是HTMLElement。复制代码代码如下:
var ifr1 = ElementById('ifr1');
var ifr1win = window.frames[0];
ifr1win.frameElement === ifr1; // true
ifr1win === ifr1; // fal