问题描述:我想要父页面打开一个页面,然后即可调用子页面的js方法,测试一下总不能成功,有没有高手能帮我解决一下这个问题呢:
例如:
father.html
<html>
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
<title>无标题文档</title>   
</head>   
<body>  
<table>   
    <tr>   
        <td>   <input type="button" value="first"/> </td>
    </tr>   
</table>   
</body>   
</html>   
<script>   
function sendEvent(){   
        var newWindow = window.open("son.html", "", "width=1024,height=768");   
        newWindow.a('a');
    } 
</script>
son.html
<html>
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
<title>homepage</title>   
</head>   
<body>  
<input type="text" value="dd" id="main_iframe"/></body>   
</html>
<script>   
function a(para)
{
alert(para );
}
</script>结果呢:这个a无法弹出来,但是如果把父页面的js内容改成:
var newWindow ;
function sendEvent(){   
        newWindow = window.open("son.html", "", "width=1024,height=768");   
    } 
function aa(){
newWindow.a('a');
}
这时候再父页面调用aa方法的时候就能弹出‘a’了,
想要的效果是在打开页面的同时就调用方法,想到了一种解决办法,就是定义子页面的onload方法,让页面加载完的时候就执行子页面中的方法,这是其中的一个方法,不知有没有高手有另外的方法,还望不吝赐教!!!
js

解决方案 »

  1.   

    关键问题在于a.htm加载慢于newWindow.a('a');这一句的执行。所以最好不要这样做。而且目前不流行弹窗,弹窗对客户不友好,一般用div在页面里模拟弹窗比较友好。逻辑也比较好处理。
      

  2.   

     newWindow = window.open("son.html", "", "width=1024,height=768");  
     setTimeout(function(){newWindow.alert(0);},1000);
      

  3.   


    也可以不延时直接调用,但会因为页面跳转被刷掉,只要页面已经跳转就可以在子页面执行js了,但调用非原生的js函数就得等js加载后才能成功