views:

446

answers:

2

Hi, I am trying to access a javascript function in a programatically created iFrame from a javacript function outside. I tried several ways, but was not successful. like window.frames['frameid']. etc. Could you please provide me a syntax,

A: 

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".

Mic
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. Would the options you mentioned would work? Please clarify me.
keiv
I would make the reverse, when the JSP is loaded, call an object of the parent. To do that, add a script tag at the very end of the BODY tag. And in this script add a command like parent.receiver.listen(msg). The object "receiver" must exist as a global object, and have a function "listen" and "msg" is a parameter you send to that function.
Mic
Mic, I have added a function in the parent JS file as, var receiver = {listen:function(){ alert('parent'); } }And added -- parent.receiver.listen(); in the script tag of the BODY tag in the JSP. From the parent JS file, I have a button onClick calling a function listen. But, it does not seem to work. Please let me know, if I am doing as you mentioned.But, I would like to call a function 'test' in the JSP from outside.Appreciate your help.
keiv
scraped the previous answer with another example
Mic
Mic,Apologize if I had confused you. I have func1() in JSP which is rendered as an iFrame in ExtJS window. And I have a button on the exterior panel on the ExtJS window,which is basically outside the iFrame. When I click on the button I am unable to call func1() function in the iFrame (JSP). Please note, the extjs iFrame is created programatically. I tried several options like window.frames[frameid] to get handle of iFrame, but could not get it. All I would request is access function in JSP from outside. Appreciate your help.
keiv
make the msg = {callBack:func1} when you call w.parent.receiver.listen(msg); and in the receiver.listen, enable the button and call msg.callBack at the onclick event
Mic
A: 

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.

keiv