Solved

Trigger a JS function in the parent window from an iFrame

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

730 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