开发者

Access JavaScript function in a programatically iFrame

开发者 https://www.devze.com 2022-12-19 13:37 出处:网络
I am trying to access a JavaScript function in a programatically created iFrame from a JavaScript function outside. I tried several ways, but was not successful. L ike window.frames[\'frameid\'开发者_

I am trying to access a JavaScript function in a programatically created iFrame from a JavaScript function outside. I tried several ways, but was not successful. L ike window.frames['frameid'开发者_如何学C], etc.

Could you please provide me the correct syntax?


Yes, they are in the same domain. Actually, I am using ExtJS framework. I have a JSP inside an iFrame, I would like to call the javascript functions in the JSP from the a javascript function outside which is basically in a JS file.


Here's a more appropriate example based on your comments ;)

The main page

<html>
<head>
    <title>Main Page</title>
</head>
<body>
    <iframe src="iframe.html"></iframe>
    <script>
        var receiver = {
          listen:function(msg){
            alert(msg);
          }
        };
    </script>
</body>
</html>

The iframe page: iframe.html, but can be a JSP with similar output

<html>
<head>
    <title>iframe page</title>
    <script src="external.js"></script>
</head>
<body>
    <!-- some content here -->
    <script>
        externalFunction('hello', window);
    </script>
</body>
</html>

And the JS file: external.js

function externalFunction(msg, w){
    w.parent.receiver.listen(msg);
}

Place those 3 files in the same directory and open the main page. You should get a popup with "hello".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号