By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Accessing iframe body from parent

Jezternz
100+
P: 145
Simple enough, I want to access the body of an iframe from the parent window.
The thing that makes this hard is I create the iframe dynamicly, aparently some of the methods I have been using only work when the frame is made when the document is loaded first.. Also note I need this to be able to run in FF and IE7

Expand|Select|Wrap|Line Numbers
  1. var board = document.createElement('iframe');    
  2. board.id = 'board';
  3. board.name = 'board';
  4.  
The following does not work (have looked over the net for a few things):

Expand|Select|Wrap|Line Numbers
  1. board.document.body.backgroundColor = red;
  2. board.contentWindow.document.body.backgroundColor = red;
  3. window.frames['board'].document.body.backgroundColor = red;
  4.  
Hopefully this is simple, thanks in advance, Josh
Dec 20 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
You can also use contentDocument where supported. To change the background, use style.backgroundColor.
Dec 20 '08 #2

Jezternz
100+
P: 145
oops, my bad, I meant to put the ".style" in.
I tried using contentDocument it didnt seem to work.

Expand|Select|Wrap|Line Numbers
  1. var board = document.createElement('iframe');    
  2. board.id = 'board';
  3. board.name = 'board';
  4. document.body.appendChild(board);
  5. var thatiframe = board.contentDocument.body;
  6.  
I get the following error:
Error 'board.contentDocument.body' is null or not an object.

Any ideas?

By the way thanks alot acoder, you always seem to be very helpful, and go out of your way to help, cheers. Also thanks to the other experts.
Dec 21 '08 #3

Jezternz
100+
P: 145
Simple Fix:

Expand|Select|Wrap|Line Numbers
  1. theiframebody = (iframe_el.contentDocument)?
  2.  iframe_el.contentDocument.body:
  3.   iframe_el.Document.body;
  4.  
You need to use 'Document' for IE :S

thanks, Josh
Dec 22 '08 #4

acoder
Expert Mod 15k+
P: 16,027
Hmm, that's strange. It should be contentWindow.document for IE. However, I think I know what might be the cause. If you have this straight after appending, t may not be ready straight away (and hence undefined), so either use a timeout or call it iframe onload.
Dec 22 '08 #5

Jezternz
100+
P: 145
ahhh that would make sence.
cheers.
Dec 22 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.