473,387 Members | 1,882 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,387 software developers and data experts.

get the id of the iframe that my content loaded in

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
4 27972

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alexander | last post by:
Scroll IFrame Content to desired Position (JavaScript) ====================================== How can I scoll the content of an Ifram to a desire Position? The function should be:...
4
by: Paul Brant | last post by:
Hi all, I have a page with an IFRAME in it. From a script in the main ( parent ) page I instruct the IFRAME to load a specific URL. The URL contains parameters that are processed by the server....
1
by: Alexandre Jaquet | last post by:
Hi I'm still working on iframe and now I'm looking for the way to get and update iframe content. example I want to do something like : from my mainform : ...
1
by: Eli | last post by:
Hi, I've created a dynamic IFRAME and inserted it in the document. Then I changed the src property to some URL (not the same domain as the main document). I want to check when the IFRAME is...
9
by: aatcbbtccctc | last post by:
Hi folks I've googled for an answer to this, with no success. Can someone please jump in, and put me out of my misery! (I'm sure it's quite simple) I have an invisible IFRAME, and a visible...
1
by: mike888 | last post by:
I want to create dynamic iframe content like below but in Firefox <iframe width="100%" height="100" src="#"></iframe> <script language="JavaScript"><!-- document.frames.document.open();...
3
by: coat | last post by:
hello I would like the first 200 dots of iframe content to be skipped (so the iframe content top is 200 dots from the real top). it should not be able to scroll up those 200 dots. how can that be...
5
by: perhapscwk | last post by:
is there any ways to modify output in a iframe content? so let say if I have a website, now due to some reasons, i need to makesure all words like "the" become "xxx", if we can chnage all words in...
2
by: isarajeev | last post by:
I have a aspx page, say 'abc.aspx'. Within that page i have 4 tab controls. The tab controls open as iframe. I also have a print option on my page.(an anchor) <a id="aPrint" runat="server"...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.