3

I am using an iFrame in my application. Let me give an example here. I have main page as

<html>
  <head>
    <script src='jquery.js'></script>
    <script>
      function TestFunction()
      {
        var FirstName = $("#first_name").val();
        alert(FirstName);
      }
    </script>
  <head>
  <body>
    Enter First Name: <input type='text' size='20' id='first_name'><br />
    <input type='button' value='Cilck' onclick='TestFunction();'><br />
    <iframe id='test_iframe' src='test_iframe.htm' height='200' width='200'>
    </iframe>
  </body>
</html>

...and this works fine with alerting whatever is entered in textbox

But is it possible to invoke the same function in iframe that will alert the value present in textbox of the parent page?

Suppose test_iframe.htm code is like this

<html>
  <head>
  <script src='jquery.js'></script>
  <script>
     function IframeFunction()
     {
        TestFunction(); // I know this wont work.. just an example
     }
  </script>
  <head>
  <body>
    <input type='button' value='Click' onclick='IframeFunction();'>
  </body>
</html>

Can this be done ?

skos
  • 4,102
  • 8
  • 36
  • 59
  • possible duplicate of [Iframe Function Calling From Iframe to parent page javascript function](http://stackoverflow.com/questions/2161388/iframe-function-calling-from-iframe-to-parent-page-javascript-function) – Jan Hančič Apr 19 '12 at 11:38

3 Answers3

4

I believe this can be done with 'parent'

function IframeFunction() 
{ 
   parent.TestFunction(); 
}
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
1
<html>
  <head>
  <script src='jquery.js'></script>
  <script>
     function IframeFunction()
     {
       parent.TestFunction();  // or top.TestFunction()
     }
  </script>
  <head>
  <body>
    <input type='button' value='Click' onclick='IframeFunction();'>
  </body>
</html>
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0
<a onclick="parent.abc();" href="#" >Call Me </a>

See Window.Parent

Returns a reference to the parent of the current window or subframe.

If a window does not have a parent, its parent property is a reference to itself.

When a window is loaded in an , , or , its parent is the window with the element embedding the window.

This answer is taken from stackoverflow

Community
  • 1
  • 1
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110