473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
Mar 11 '08 #1
7 1691
gits
5,390 Expert Mod 4TB
try:

Expand|Select|Wrap|Line Numbers
  1. window.frames['your_frame_name'].document.getElementById('img_id')
kind regards
Mar 11 '08 #2
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
Mar 11 '08 #3
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
Mar 11 '08 #4
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.
Mar 12 '08 #5
gits
5,390 Expert Mod 4TB
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
Mar 12 '08 #6
Yes! This works.

Thank you for your help.
Arman
Mar 12 '08 #7
gits
5,390 Expert Mod 4TB
glad to hear that :) ... post back to the forum anytime you have more questions ...

kind regards
Mar 12 '08 #8

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

Similar topics

3
by: Magnus | last post by:
I have a iframe inside a main html file. The iframe is used to display various html files. Inside the html files displayed in the iframe I want to have javascript to set certain global cookies,...
7
by: ABC | last post by:
I am a total newbie to javascript. I have only done Frontpage. I am trying to do a page of image show----I only managed to find a javascript to create pop up windows for the chosen image. Almost...
5
by: Tudor Tihan | last post by:
Hi, This is my first post here, so please be kind. I have tryed to make a javascript html page loader by using an invisible <IFrame> and some javascript variable text passing between...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.