Connecting Tech Pros Worldwide Forums | Help | Site Map

JavaScript: How to access an image in main doc from iframe

Newbie
 
Join Date: Mar 2008
Location: Netherlands
Posts: 4
#1: Mar 11 '08
Please help on the following:

I have a document loaded in an IFRAME. In that document, I need in a JavaScript to access an image (having an ID) in the main document (the document including the IFRAME). What I want to do is to change the image source in the main document.

My question: what is the (DOM) code to access that image. Is the .referrer used here?

Thanks in advance, Arman

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Mar 11 '08

re: JavaScript: How to access an image in main doc from iframe


try:

Expand|Select|Wrap|Line Numbers
  1. window.frames['your_frame_name'].document.getElementById('img_id')
kind regards
Newbie
 
Join Date: Mar 2008
Location: Netherlands
Posts: 4
#3: Mar 11 '08

re: JavaScript: How to access an image in main doc from iframe


Sorry, it's doesn't work.

I need in the IFRAME document the image in the parent. Thus, this refers to an image in the frame (at least, that's what I understand):
Expand|Select|Wrap|Line Numbers
  1. window.frames['your_frame_name'].document.getElementById('img_id').src
This also doesn't work:
Expand|Select|Wrap|Line Numbers
  1. window.parent.document.getElementById('img_id').src
Please help once more and explain why the above example is wrong.

Thanks, Arman
Newbie
 
Join Date: Feb 2008
Location: Boston now, Back home to Vermont soon!
Posts: 14
#4: Mar 11 '08

re: JavaScript: How to access an image in main doc from iframe


Quote:

Originally Posted by armanic

Sorry, it's doesn't work.

I need in the IFRAME document the image in the parent. Thus, this refers to an image in the frame (at least, that's what I understand):

Expand|Select|Wrap|Line Numbers
  1. window.frames['your_frame_name'].document.getElementById('img_id').src
This also doesn't work:
Expand|Select|Wrap|Line Numbers
  1. window.parent.document.getElementById('img_id').src
Please help once more and explain why the above example is wrong.

Thanks, Arman

I can't tell what exactly what you are trying to do here, but this is how I'd do it.

Expand|Select|Wrap|Line Numbers
  1. window.frames['frame_name'].document.getElementById("the_id").src = parent.document.getElementById("the_image_id").src;
IE has given me trouble with that in the past so I had to do it like this.

Expand|Select|Wrap|Line Numbers
  1. var my_iframe = window.frames['frame_name'];
  2. var the_src = parent.document.getElementById("image_id").src;
  3. my_iframe.document.getElementById("the_iframe_image_id").src = the_src;
I hope this helps if not maybe I'm not understanding what you are trying to do. Good day,
CpVermont
Newbie
 
Join Date: Mar 2008
Location: Netherlands
Posts: 4
#5: Mar 12 '08

re: JavaScript: How to access an image in main doc from iframe


Let me explain what I'm trying to do:

There is a document ("doc1") that contains an image ("img1") and an IFRAME ("iframe1") . In this IFRAME, a second document is loaded ("doc2").
In doc2, I want to change the source of img1 in doc1.

When doing this in doc1, the code will be:
Expand|Select|Wrap|Line Numbers
  1. function changeSrc()
  2.   {
  3.   document.getElementById("img1").src="any_image.gif"
  4.   }
The question is how this code must be when it is located in doc2.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#6: Mar 12 '08

re: JavaScript: How to access an image in main doc from iframe


sorry for the misunderstanding ... it should work with:

Expand|Select|Wrap|Line Numbers
  1. parent.document.getElementById('img1').src = 'any_image.gif';
assuming that the iframe is part of the doc1?

kind regards
Newbie
 
Join Date: Mar 2008
Location: Netherlands
Posts: 4
#7: Mar 12 '08

re: JavaScript: How to access an image in main doc from iframe


Yes! This works.

Thank you for your help.
Arman
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#8: Mar 12 '08

re: JavaScript: How to access an image in main doc from iframe


glad to hear that :) ... post back to the forum anytime you have more questions ...

kind regards
Reply