Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  124] [ 6]  / answers: 1 / hits: 91222  / 13 Years ago, wed, january 11, 2012, 12:00:00

I'm currently trying to customize OpenCms (java-based open source CMS) a bit, which is using the FCKEditor embedded, which is what I'm trying access using js / jQuery.



I try to fetch the html content of the iframe, however, always getting null as a return.
This is how I try to fetch the html content from the iframe:



var editFrame = document.getElementById('ta_OpenCmsHtml.LargeNews_1_.Teaser_1_.0___Frame');
alert( $(editFrame).attr('id') ); // returns the correct id
alert( $(editFrame).contents().html() ); // returns null (!!)


Looking at the screenshot, the what I want to access is the 'LargeNews1/Teaser' html section, which currently holds the values Newsline en....
Below you can also see the html structure in Firebug.



However, $(editFrame).contents().html() returns null and I can't figure out why, whereas $(editFrame).attr('id') returns the correct id.



The iframe content / FCKEditor is on the same site/domain, no cross-site issues.



enter



HTML code of iframe is at http://pastebin.com/hPuM7VUz



Updated:



Here's a solution that works:



var editArea = document.getElementById('ta_OpenCmsHtml.LargeNews_1_.Teaser_1_.0___Frame').contentWindow.document.getElementById('xEditingArea');                        
$(editArea).find('iframe:first').contents().find('html:first').find('body:first').html('some <b>new</b><br/> value');

More From » jquery

 Answers
23

.contents().html() doesn't work to get the HTML code of an IFRAME. You can do the following to get it:



$(editFrame).contents().find(html).html();


That should return all the HTML in the IFRAME for you. Or you can use body or head instead of html to get those sections too.


[#88090] Tuesday, January 10, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uriahw

Total Points: 372
Total Questions: 93
Total Answers: 115

Location: Bahrain
Member since Fri, Sep 16, 2022
2 Years ago
;