?
Solved

Trigger a JS function in the parent window from an iFrame

Posted on 2008-10-04
2
Medium Priority
?
1,301 Views
Last Modified: 2012-05-05
Hiya,

I have a page set up as below, and I'm trying trigger the 'sayMessage' javascript function from a link in the page within the iframe (page.html).

Is this possible?

Thanks.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
 
<script type="text/javascript">
function sayMessage(text) {
  alert(text);
}
</script>
</head>
 
<body>
 
<iframe name="iframe1" id="iframe1" frameborder="0" width="270" height="200" scrolling="no" src="page.html"></iframe>
 
</body>
</html>

Open in new window

0
Comment
Question by:Mr_Splash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Expert Comment

by:JMT116
ID: 22640700
This site has a tutorial about what you are trying to do.  
http://www.ozzu.com/programming-forum/argh-calling-parent-functions-from-page-within-iframe-t21672.html

I think that both parent and Iframe page need to be in the same domain/site.
<!-- Parent Code -->
<script language="Javascript">
function testIframe() {
 alert('Called from Iframe!'); 
}
</script>
 
<iframe src="testIframe.html"></iframe>
 
 
<!-- IFRAME CODE -->
<script language="Javascript">
  parent.testIframe();
</script>

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22640815
>>Is this possible?
Only if both pages are from your own domain. Otherwise it would be cross-site scripting, and the browser would NOT allow it. To call your function from the iframe you can use:
<a onclick="top.sayMessage('Hi');"...>...</a>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question