Solved

Trigger a JS function in the parent window from an iFrame

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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-…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now