<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Page</title> <style type="text/css">... <!-- body{...}{ margin:0px; } --> </style> </head> <body> <iframe id="ifr" name="ifr" width="100%" height="500" src="bb.htm"></iframe> </body> </html> <script language="javascript" type="text/javascript">... var aa_value="I'm a variant in Top window!"; var ifr_id=document.getElementById("ifr"); var ifr_window=window.frames["ifr"]; alert("Alert from Top window : Can't get iframe's variant by ifr_id, it will return :"+ ifr_id.bb_var); alert("Alert from Top window : Can't get iframe's DOM model by ifr_id ,it will return :"+ ifr_id.window); alert("Alert from Top window : Get src from id :"+ ifr_id.src); alert("Alert from Top window : Get href from window:"+ ifr_window.document.location.href); //由于bb.htm可能还未装载完成,所以,下冇的可能引发错误 //调用iframe内的函数 ifr_window.bb(); //调用iframe内的变量 alert("Alert from Top window : "+ ifr_window.bb_var); // alert("Alert from Top Window :"+ ifr_window.document.body.innerHTML); function aa(msg)...{ alert("I'm alerting from Top window ,and I received a msg: "+ msg); } </script>
bb.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>sub frame</title> <style type="text/css">... <!-- html,body{...}{ margin:0px; width:90%; } --> </style> </head> <body> I'm a sub frame! <br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ...<br /> ... </body> </html> <script language="javascript" type="text/javascript">... var bb_var="I'm a variable in ifr"; function bb()...{ alert("Alert from iframe :I'm frame ifr's function") } //获取父页面的变量 alert("Alert from iframe parent.ifr_id::"+ parent.ifr_id); alert("Alert from iframe parent.aa_value : "+ parent.aa_value); //通过父页面的ifr_id来改变 iframe的高度 alert("Alert from iframe : ifr's clientHeight :"+document.body.clientHeight); parent.ifr_id.height=document.body.clientHeight; alert("Alert from iframe : ifr's scrollHeight : "+ document.body.scrollHeight); //调用父窗体的函数: parent.aa("I will calling a function which is Top window's "); //改变父窗体的标题: alert("Alert from iframe : I will changing Top window's title"); top.document.title="The title value changed"; //通过父窗体的ifr_id来改变的border 与scrolling alert("Alert from iframe : I will change my border and scrolling :"); top.ifr_id.border=0; top.ifr_id.scrolling="no"; </script>