在java script中能不能访问其他窗口定义的方法?

解决方案 »

  1.   

    只要窗口之间有关系就可以。
    对于用win = window.open()打开的窗口:
    父窗口访问子窗口的方法 win.xx();
    子窗口访问父窗口的方法 opener.xx();对于iframe
    父窗口访问子窗口的方法 document.getElementById("iframe_id").contentWindow.xx(); 或iframe_name.xx();
    子窗口访问父窗口的方法 parent.xx();对于 frameset
    frame之间  top.frame_name.xx();
    父窗口访问frame的方法  frame_name.xx();
    frame访问父窗口的方法 top.xx();
      

  2.   

    可以的,在不跨域的情况下,楼上说的都可以补充一种 :showModalDialog() 弹出模态窗,调用方法也类似
    <html>  
    <head>  
    <script   type="text/javascript">  function say() { 
       alert("parent.html------>I'm at parent.html"); 
       } function callChild() 
    {  
       //document.frames("myFrame").f1(); 
       myFrame.window.say(); 
    }  
    </script>  
    </head>  
       
    <body>    
    <input   type=button   value="调用child.html中的函数say()" onclick="callChild()"> 
    <iframe name="myFrame" src="child.html"></iframe> 
    </body>  
    </html>  
    <html>  
    <head>  
    <script type="text/javascript"> 
         
    function say()  
    {  
              alert("child.html--->I'm at child.html");  
    } function callParent() { 
       parent.say(); 
       } 
    </script>  
    </head>  
    <body>  
    <input   type=button   value="调用parent.html中的say()函数"   onclick="callParent()">  
    </body>  
    </html>