JavaScript function call for parent page in iframe

I am using iFrame in my application. Let me give you an example here. I have a homepage like

<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 great with a warning about what is entered in the text box

But is it possible to call the same function in the iframe, which will warn the value present in the text field of the parent page?

Assume the code test_iframe.htm is similar to 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?

+3
source share
3 answers

I believe that this can be done using the "parent"

function IframeFunction() 
{ 
   parent.TestFunction(); 
}
+4
source
<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>
+1
source
<a onclick="parent.abc();" href="#" >Call Me </a>

. Window.Parent

.

, .

When a window loads in a, or, its parent is a window with an element, a window attachment.

This answer is taken from fooobar.com/questions/28456 / ...

0
source

All Articles