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

get the id of the iframe that my content loaded in

P: n/a
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

Oct 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

ch**************@gmail.com wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 15 '05 #2

P: n/a
ch**************@gmail.com wrote:
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[i];
if (x.src && x.src == myURL){
return 'The iframe ' + ((x.id)?
'has ID=' + x.id : 'is anonymous');
}
}
return 'Couldn\'t find the iframe';
}
</script>
--
Rob
Oct 15 '05 #3

P: n/a
RobG wrote:
ch**************@gmail.com wrote:
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[i];
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[i];
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>
--
Rob
Oct 15 '05 #4

P: n/a
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

Oct 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.