Solved

Trigger a JS function in the parent window from an iFrame

Posted on 2008-10-04
2
1,300 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 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

688 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