如何访问Iframe的父窗口的方法[Chrome浏览器] [英] How to access a method of parent window of an Iframe [Chrome browser ]

查看:87
本文介绍了如何访问Iframe的父窗口的方法[Chrome浏览器]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多次和很多方法来从 iframe 中调用方法,但尚未成功。请参阅下面的内容,


  1. main.html:


  2. iframe-1与index.html链接,从中我想调用main.html的方法或想要更改第二个iframe的src。 / b>

main.html

 < HTML> 
< head> < /头>
< body>
< iframe id =iframe-1src =index.html>< / iframe>
< iframe id =iframe-2>< / iframe>
< / body>
< / html>

index.html

 < html> < HEAD> 
< script type =text / javascript>
$(document).ready(function(){
//如何访问main.html的方法并更改iframe src
});
< / script>
< / head> <身体GT;
......
< / body> < / HTML>

注意:尝试 parent.methodName() window.parent.methodName()无法使用

@EDIT:IE& MOZILLA但在Chrome上出现错误(无法调用未定义的方法'getElementById')

解决方案

index.html

 < html> 
< head>
< script type =text / javascript>
function run(){
window.parent.document.getElementById(iframe-2)。src =/test.html;
}
< / script>
< / head>
< body onload =run();>
< / body>
< / html>

如何?

或者创建main.html中的一个方法,并使用它:
window.parent。 methodname ();



Ron。



ps。 window.parent的方法名();在main.html中有一个方法时,完美地适用于我

main.html

 < HTML> 
< head> < /头>
< script>
函数foo(){
alert(1);
}
< / script>
< body>
< iframe id =iframe-1src =index.html>< / iframe>
< iframe id =iframe-2>< / iframe>
< / body>
< / html>


I tried many times and many ways to call the method from inside the iframe while not yet successful to do so. please see below,

  1. main.html : consisting the two iframe

  2. iframe-1 linked with a index.html from where i want to call a method of main.html or want to change the src of second iframe.

main.html

<html> 
    <head> </head>
    <body>
     <iframe id="iframe-1" src="index.html"></iframe>
     <iframe id="iframe-2" ></iframe>
    </body>
</html>

index.html

<html> <head> 
<script type="text/javascript">
 $(document).ready(function(){
    // How to access the method of main.html and change the iframe src
 });
</script>
</head> <body>
    ......
</body> </html>

Note : tried parent.methodName(), window.parent.methodName() not working

@EDIT : success on IE & MOZILLA but getting error on Chrome ( Cannot call method 'getElementById' of undefined )

解决方案

index.html

<html>
<head> 
    <script type="text/javascript">
        function run() {
            window.parent.document.getElementById("iframe-2").src = "/test.html";
        }
    </script>
</head>
<body onload="run();">
</body>
</html>

How about this?

Or create a method inside main.html and access it using: window.parent.methodname();

Ron.

ps. window.parent.methodname(); works perfectly for me when I have a method in main.html

main.html

<html> 
    <head> </head>
    <script>
        function foo() {
            alert(1);
        }       
    </script>
    <body>
       <iframe id="iframe-1" src="index.html"></iframe>
       <iframe id="iframe-2" ></iframe>
    </body>
</html>

这篇关于如何访问Iframe的父窗口的方法[Chrome浏览器]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆