1

I have a html file which contains iframe like below lines of code. Note that this iframe is displayed by one of tiny mce jquery and is rendered in browser as below

 <html>
 <body>
   <textarea id="texteditor"></textarea>
   <div class="mceeditor">
     <iframe>
       <html>
         <body>
           <script type="text/javascript">

     function mySubmit() {
         var URL = "http://localhost:61222/14CommunityImages/hands.png";

         window.document.getElementById("texteditor").value = URL;
               }
          </script>
          </body>
       </html>  
     </iframe>
    </div>
   </body>
 </html>

Now my goal is to append var url inside text area which is in parent html tag. Please help me !!!

1 Answers1

1

The document in the iframe can access its parent window via parent, and its parent window's document via parent.document. So:

parent.document.getElementById("texteditor").value = URL;

Note: To access each-other's documents, the main document and the iframe must be on the same origin. If they're on different origins, they can still communicate, but only if they both do so expressly, via web messaging.


Side note: Your iframe, as shown in the question, won't work. Inline content in iframes is for display when the browser doesn't support iframes. You use a separate resource (e.g., page) identified by the src attribute (or you use the srcdoc attribute; I have no idea how well supported it is), for the iframe's content.

E.g.:

Main page:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Main Page</title>
<body>
<textarea id="texteditor"></textarea>
<div class="mceeditor">
    <iframe src="theframe.html"></iframe>
</div>
</body>
</html>

theframe.html:

<!doctype html>
<html>
    <body>
        <input type="button" value="Click to set" onclick="mySubmit()">
        <script type="text/javascript">
            function mySubmit() {
            var URL = "http://localhost:61222/14CommunityImages/hands.png";

            parent.document.getElementById("texteditor").value = URL;
            }
        </script>
    </body>
</html>  
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875