Use jQuery to embed HTML in an iFrame body tag

I am using a hosted CMS that displays an iFrame in another iFrames. These iFrames are loaded from the same domain, but since this is a hosted CMS, I do not have direct access to these iFrames. Is it possible using jQuery to embed HTML content in an bodyiFrame tag with an identifier re_contentIframe?

This is how the code is displayed on my website:

<div class="editor">
  <iframe id="editorf"  frameborder="0" src="/ForumEditor.aspx?ForumID=2221&TopicID=-1&NoTemplate=False&Internal=False" style="display: inline;">
    <html>
      <head></head>
        <body>
          <!-- CODE -->
            <iframe id="re_contentIframe" frameborder="0" src="javascript:'<html></html>';">
             <html>
              <head></head>
               <body> <!-- THIS IS WHAT I WANT TO TARGET --> </body>
             </html>
            </iframe>
          <!-- CODE -->
      </body>
    </html>
  </iframe>
</div>

I tried using the following code, but nothing happens with this code (including errors):

$(function() {
   $( "#test" ).click(function() {
       $("#re_contentIframe").contents().find("body").html("<p>new HTML content goes here</p>");
   });
});
+4
source share
2 answers

The problem is that you are trying to access a nested frame.

#re_contentIframe #editorf, .

Try:

$('#re_contentIframe').contents().find('body').find('#editorf').contents()

Fiddle: http://jsfiddle.net/8VP4y/3/

+4

, :

var frameObject = document.getElementById('editorf');
var frameContent = frameObject.contentDocument ||
frameObject.contentWindow.document;
var insideIframe = frameContent.getElementById('re_contentIframe');
var insideIframeContent = insideIframeObject.contentDocument ||
insideIframeObject.contentWindow.document;
var $body = $('body',insideIframeContent);
$body.html('<div>contentupdated</div>');
0

All Articles