4

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 ?

1

3 Answers 3

4

I believe this can be done with 'parent'

function IframeFunction() 
{ 
   parent.TestFunction(); 
}
Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.