Solved

Trigger a JS function in the parent window from an iFrame

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

786 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