js调用iframe里面js函数

<!doctype html>
<html>
<head> <meta charset="utf-8"> <title>js invoke iframe inner function</title> <style> *{ margin: 0; padding:0;} </style> </head> <body> <iframe id="testframe" name="testframe" src="./fanye.html" height="100%" width="100%" frameborder="0" scrolling="no" frameborder="no" marginheight="0" marginwidth="0" style="background-color:#9FC"></iframe> <script> function test(){ //下面三种方法等同,myalert()是jstest.html页面中的一个函数 //window.frames['testframe'].frameElement.contentWindow.myalert(); parent.window.frames[0].frameElement.contentWindow.myalert(); //document.getElementById("testframe").contentWindow.myalert(); } //当页面加载完成后执行这个函数,如果iframe的页面没有加载完成,test方法中执行myalert()的时候很可能会找不到这个函数 document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function subSomething() { if(document.readyState == "complete") {//当页面加载状态 test(); } } </script> </body> </html> 需要注意的是,iframe中的页面应该是同一个域名下的,不然会出现跨域的情况

猜你喜欢

转载自www.cnblogs.com/raincowl/p/9051130.html