Invoke IFrame/window in cross domain in IE&FF notes

本文详细解析了网页中iframe元素的使用,包括如何在HTML页面中创建和配置iframe,以及如何在不同浏览器环境下访问和操作iframe内的元素。此外,还介绍了iframe在不同浏览器间的异步通信方法,包括在IE、Firefox等浏览器中通过document.frames和contentWindow获取和修改iframe内元素的方法。同时,文章还涵盖了iframe的自动高度调整、跨窗口通信以及跨iframe通信的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Define Iframe in page:

In html page:

<iframe id = ' iId ' name = ' iName ' src = ' srcPage.html ' scrolling = ' no ' frameborder = ' 0 ' ></ iframe >

In javascript:

var frame = document.createElement("iframe");

frame.src = "iframePage.html";

document.body.appendChild(frame);

 

Invoke element of iframe in IE:

1. Through document.frames["IframeName"]:

alert ( document . frames [ " iName " ] . document . getElementsByTagName ( ' h1 ' )[ 0 ] . firstChild . data ) ;

2. Use contentWindow to get it:

window . onload = ( function () {
var iObj = document . getElementById ( ' iId ' ) . contentWindow ;
alert ( iObj . document . getElementsByTagName ( ' h1 ' )[ 0 ] . firstChild . data ) ;
}) ;

3. Update the content of element of iframe:

iObj . document . getElementsByTagName ( 'h1' )[ 0 ] . innerHTML = "override cotent of element" ;

 

Invoke element of iframe in FF:

var iObj = document . getElementById ( ' iId ' ) . contentWindow ;
iObj . document . open () ;
iObj . document . writeln ( ' <html><head> ' ) ;
iObj . document . writeln ( ' <style>body {background:#000;}</style> ' ) ;
iObj . document . writeln ( ' </head><body></body></html> ' ) ; iObj . document . close () ;

 

Iframe auto-height:

window . onload = ( function () {
var iObj = document . getElementById ( ' iId ' ) ;
iObj . height = iObj . contentWindow . document . documentElement . scrollHeight ; }) ;

 

Cross Window

ParentWindow.html:

var obj = {"prop1":"Hello","prop2":"Whats Up","prop3":{"subProp1":"Hi","subProp2":"Yeah Yeah"}};
obj["func1"] = function(){ alert("this is a test"); };
var w = window.open("childWindow.html","newWindow");
w.setObj(obj);
w.alertObj();
alert(typeof w.childObj["func1"]);

ChildWindow.html:

var childObj;
window.setObj = function(o) {
    childObj = o;
}
window.alertObj = function() {
    alert(typeof childObj["func1"]);
}

// 1. Firstly will alert object in childWindow.html

// 2. Secondly will alert function in parentWindow.html

 

Cross Iframe:

Parent Window.html:

<html>
<head></head>
<body></body>
<script type="text/javascript">
    var obj = { "prop1": "Hello", "prop2": "Whats Up", "prop3": { "subProp1": "Hi", "subProp2": "Yeah Yeah"} };
    obj["func1"] = function () {
        alert("this is a test");
    };
    var frame = document.createElement("iframe");
    frame.src = "childWindow.html";
    frame.onload = function () {
        frame.contentWindow.setObj(obj);
        frame.contentWindow.alertObj();
        alert(typeof frame.contentWindow.childObj["func1"]);
    }
    document.body.appendChild(frame);
</script>
</html>

ChildWindow.html:

<script type="text/javascript">
    var childObj;
    window.setObj = function (o) {
        childObj = o;
    }
    window.alertObj = function () {
        alert(typeof childObj["func1"]);
    }
</script>

// 1. Firstly will alert function

// 2. Secondly will alert function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值