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
2010-02-03 08:12:24
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
2010-02-03 19:58:13
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
2010-02-03 21:16:40
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
2010-02-03 22:46:41
scraped the previous answer with another example
Mic
2010-02-03 23:18:26
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
2010-02-04 04:45:21
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
2010-02-04 08:19:22
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
2010-02-03 19:56:59