get the id of the iframe that my content loaded in

C

christine.nguyen

Hello,

Here's the deal. I have an html page that contains an iframe. This
iframe has an id that is unknown to me. This iframe also has test.html
as it's source. Within test.html runs some javascript. Within this
javascript that runs in test.html, I want to obtain the id of the
iframe that loaded test.html in the first place. Is this possible?? I
have no control over the iframe at all. The iframe will be loaded on
some random html page that I have no control over. I only have control
over test.html and its contents, but I need to get the id of that
iframe that loaded test.html anyway.

Make sense???

Any help is appreciated.

Thanks!
-chh
 
M

Martin Honnen

I have an html page that contains an iframe. This
iframe has an id that is unknown to me. This iframe also has test.html
as it's source. Within test.html runs some javascript. Within this
javascript that runs in test.html, I want to obtain the id of the
iframe that loaded test.html in the first place.

IE (on Windows since IE 5.5.), Mozilla and recent Opera support
window.frameElement
which gives you the the <frame> or <iframe> element corresponding to the
window element containing the loaded frame document.
Thus in those browsers
window.frameElement.id
should give you what you are looking for.

Besides that you can certain access
parent.document.getElementsByTagName('iframe')
and loop through to find the matching <iframe> element.

But cross frame scripting is subject to the same origin policy thus all
those approaches do only work if the container document and your frame
document are loaded from the same origin.
 
R

RobG

Hello,

Here's the deal. I have an html page that contains an iframe. This
iframe has an id that is unknown to me. This iframe also has test.html
as it's source. Within test.html runs some javascript. Within this
javascript that runs in test.html, I want to obtain the id of the
iframe that loaded test.html in the first place. Is this possible?? I
have no control over the iframe at all. The iframe will be loaded on
some random html page that I have no control over. I only have control
over test.html and its contents, but I need to get the id of that
iframe that loaded test.html anyway.

Make sense???

Any help is appreciated.

'top' gets you the window containing the iframe that your page is loaded
in. The following script gets the href of your page, goes to 'top',
gets the iframe elements, then looks for one with a src attribute that
matches your page.

It stops at the first match (if there is one). If you think your page
may be loaded in more than one iframe (I can't see why that makes sense,
but anyhow) you may want to keep looking.

Put this in your page:

<input type="button" value="Get iFrame ID" onclick="
alert(getIframeID(this));
">

<script type="text/javascript">
function getIframeID(el)
{
var myTop = top;
var myURL = location.href;
var iFs = top.document.getElementsByTagName('iframe');
var x, i = iFs.length;
while ( i-- ){
x = iFs;
if (x.src && x.src == myURL){
return 'The iframe ' + ((x.id)?
'has ID=' + x.id : 'is anonymous');
}
}
return 'Couldn\'t find the iframe';
}
</script>
 
R

RobG

RobG said:
Hello,

Here's the deal. I have an html page that contains an iframe. This
iframe has an id that is unknown to me. This iframe also has test.html
as it's source. Within test.html runs some javascript. Within this
javascript that runs in test.html, I want to obtain the id of the
iframe that loaded test.html in the first place. Is this possible?? I
have no control over the iframe at all. The iframe will be loaded on
some random html page that I have no control over. I only have control
over test.html and its contents, but I need to get the id of that
iframe that loaded test.html anyway.

Make sense???
Any help is appreciated.


'top' gets you the window containing the iframe that your page is loaded
in. The following script gets the href of your page, goes to 'top',
gets the iframe elements, then looks for one with a src attribute that
matches your page.

It stops at the first match (if there is one). If you think your page
may be loaded in more than one iframe (I can't see why that makes sense,
but anyhow) you may want to keep looking.

Put this in your page:

<input type="button" value="Get iFrame ID" onclick="
alert(getIframeID(this));
">

<script type="text/javascript">
function getIframeID(el)
{
var myTop = top;
var myURL = location.href;
var iFs = top.document.getElementsByTagName('iframe');
var x, i = iFs.length;
while ( i-- ){
x = iFs;
if (x.src && x.src == myURL){


Ooops... this may not work.

Firefox reports a full path for the src attribute even if a relative
path has been used, IE reports only the relative bit. So it will not
match the location.href of the file inside the frame.

Matching just filenames may be deadly - particularly if yours is
'index.html' or 'default.html' or any of the popular names.

If you know that full paths are being used, you're sweet. However, if
relative paths are in use, you may have some difficulty.

Using window.frameElement as suggested by Martin gives a possible out,
the sample below is tested in Firefox & IE:


<script type="text/javascript">
function getIframeID(el)
{
var myTop;
if (window.frameElement) {
myTop = window.frameElement;
} else if (window.top) {
myTop = window.top;
var myURL = location.href;
var iFs = myTop.document.getElementsByTagName('iframe');
var x, i = iFs.length;
while ( i-- ){
x = iFs;
if (x.src && x.src == myURL){
myTop = x;
break;
}
}
}
if (myTop){
return 'The iframe ' + ((myTop.id)?
'has ID=' + myTop.id : 'is anonymous');
} else {
return 'Couldn\'t find the iframe';
}
}
</script>
 
C

christine.nguyen

Thanks everybody for the responses. I found the frameElement propery
right after I posted. Isn't that always how it works?? Anyway, for
browsers that don't support frameElement, I don't want to do something
like check the src attribute to see if it matches because my content
may be loaded in more than one iframe on a page. So, what I've done is
create a global javascript variable and assign it a random number value
when my document loads in the given iframe. Within my document, I then
loop through the parent's frames collection and find the corresponding
iframe by matching up the random number value in the global javascript
variable. Once I find the appropriate iframe based on the random
number value, I get the iframe's id. I'm just telling y'all this just
in case anybody ever has the same problem and comes across my post.

Thanks again,
chh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top