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

Problems trying to access an iframe within an iframe

P: 11
I've got an iframe (id='vidiframe') that holds an iframe (id='ytvid') and I'd like to have a link (the link is in a separate iframe too) that would change the contents of that inner-most iframe, but I can figure out how to do it. Here's my code so far:

Expand|Select|Wrap|Line Numbers
  1. function ytvidChange(vid){parent.document.getElementById('vidiframe').document.getElementById('ytvid').src="http://www.youtube.com/v/"+vid}
I'm getting the error "Error: parent.document.getElementById("vidiframe").docume nt is undefined". I have tried replacing the second ".document" with ".src", but that hasn't worked either. Thanks
Jul 18 '08 #1
Share this Question
Share on Google+
8 Replies


dmjpro
100+
P: 2,476
Why are you using parent?
Use document.....
Jul 18 '08 #2

acoder
Expert Mod 15k+
P: 16,027
Where is the function called from?
Jul 18 '08 #3

P: 11
I'm using parent because the link is inside of an iframe and, from the way I understand, needs to access its parent so that it can find the other iframe it's trying to change- the link is in a separate iframe from the target.

I'm afraid I don't really understand what you mean by the function called from.
Jul 19 '08 #4

acoder
Expert Mod 15k+
P: 16,027
You've shown the function, but it has to be called from somewhere. Post the code where you make the function call. Also, I assume the function is in the same iframe from where it's called from.
Jul 19 '08 #5

P: 11
You've shown the function, but it has to be called from somewhere. Post the code where you make the function call. Also, I assume the function is in the same iframe from where it's called from.
Oh, okay I got ya- I read the first post incorrectly :p The function is being called from a separate iframe outside of the target of the function. Here's the code that calls the function:
[HTML]...<td><a id='vid1' href="javascript:ytvidChange('CLqOwiZ8n5I')" title="Wallflowers - One Headlight"><img class=ytprev src="http://i.ytimg.com/vi/CLqOwiZ8n5I/default.jpg" /></a><br /><script type="text/javascript">drawvidName('vid1')</script></td><tr>...[/HTML]
Jul 19 '08 #6

acoder
Expert Mod 15k+
P: 16,027
To access the document inside an iframe, use contentDocument/contentWindow:
Expand|Select|Wrap|Line Numbers
  1. var vidiframe = parent.document.getElementById("vidiframe");
  2. if (vidiframe.contentDocument) vidiframe.contentDocument.document...
  3. else if (vidiframe.contentWindow) vidiframe.contentWindow.document...
To access the document directly, you would need to use the window.frames[] syntax.
Jul 19 '08 #7

P: 11
To access the document inside an iframe, use contentDocument/contentWindow:
Expand|Select|Wrap|Line Numbers
  1. var vidiframe = parent.document.getElementById("vidiframe");
  2. if (vidiframe.contentDocument) vidiframe.contentDocument.document...
  3. else if (vidiframe.contentWindow) vidiframe.contentWindow.document...
To access the document directly, you would need to use the window.frames[] syntax.
Sweet it works! Thanks a bunch

Say, is there an online place where I can learn more about the way frames and iframes work more in-depth that any of you would recommend?
Jul 21 '08 #8

acoder
Expert Mod 15k+
P: 16,027
Glad to hear that it's working. Note that contentDocument doesn't require document after it except Opera I think, so you may want to test further.

Here's a couple of links that may help:
http://www.quirksmode.org/js/iframe.html
http://www.w3schools.com/HTMLDOM/dom_obj_iframe.asp
Jul 21 '08 #9

Post your reply

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