admin管理员组

文章数量:1312642

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 ?

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 ?

Share Improve this question asked Apr 19, 2012 at 11:33 skosskos 4,2428 gold badges38 silver badges59 bronze badges 1
  • possible duplicate of Iframe Function Calling From Iframe to parent page javascript function – Jan Hančič Commented Apr 19, 2012 at 11:38
Add a ment  | 

3 Answers 3

Reset to default 4

I believe this can be done with 'parent'

function IframeFunction() 
{ 
   parent.TestFunction(); 
}
<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>
<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

本文标签: jqueryCalling Parent Page JavaScript Function In An IframeStack Overflow