473,672 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DOM - get element in Iframe from parent document

I am trying to access a table in an iframe via javascript. It sounds
easy - but it won´t work...

The iframe is added to the document via someContainerEl ement.innerHTML
= "<iframe... >", it has name and ID and its visible in my DOM explorer
just as the table I need is it. The table is added from ASP.NET via
Response.Write( ).

I have tried both window.frames[], document.getEle mentById and even
document.getEle mentsByTagName, nothing has worked when the iframe is
involved...

It seems I cannot get af reference to the table in the iframe from the
parent document... ??

May 1 '06 #1
8 52819
Henrik Stidsen said the following on 5/1/2006 8:22 AM:
I am trying to access a table in an iframe via javascript. It sounds
easy - but it won´t work...
And as they say "Then you must not be doing something right" :)
The iframe is added to the document via someContainerEl ement.innerHTML
= "<iframe... >", it has name and ID and its visible in my DOM explorer
just as the table I need is it. The table is added from ASP.NET via
Response.Write( ).
It's irrelevant that it is added in ASP.NET, but, if you have ASP.NET
then why are you adding it to the document using client side scripting?
Just insert it with ASP.
I have tried both window.frames[], document.getEle mentById and even
document.getEle mentsByTagName, nothing has worked when the iframe is
involved...
It takes a combination :)
It seems I cannot get af reference to the table in the iframe from the
parent document... ??


window.frames['IFrameName'].document.getEl ementById('Tabl eID');

If that doesn't work, post a URL to a sample page that shows the behavior.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 1 '06 #2
>> I am trying to access a table in an iframe via javascript. It sounds
easy - but it won´t work...
And as they say "Then you must not be doing something right" :)
And yet, it works when the table is added directly to the parent
document and not in the iframe - so something must be right somewhere
:)
It's irrelevant that it is added in ASP.NET, but, if you have ASP.NET
then why are you adding it to the document using client side scripting?
Just insert it with ASP.
Its an old script being updated for a new world - I wish I could, but I
can´t (as in, it takes too much work...)
I have tried both window.frames[], document.getEle mentById and even
document.getEle mentsByTagName, nothing has worked when the iframe is
involved... It takes a combination :)
Been there, done that - didn´t work either :(
It seems I cannot get af reference to the table in the iframe from the
parent document... ??

window.frame s['IFrameName'].document.getEl ementById('Tabl eID');
No luck...
If that doesn't work, post a URL to a sample page that shows the behavior.


Unfortunately not possible at the moment...

I have a feeling that this ends with a lot of recoding...

May 1 '06 #3
Henrik Stidsen said the following on 5/1/2006 8:41 AM:
I am trying to access a table in an iframe via javascript. It sounds
easy - but it won´t work...
And as they say "Then you must not be doing something right" :)
And yet, it works when the table is added directly to the parent
document and not in the iframe - so something must be right somewhere
:)


Well, something is still wrong and it is something else in your code
causing the problems.
It's irrelevant that it is added in ASP.NET, but, if you have ASP.NET
then why are you adding it to the document using client side scripting?
Just insert it with ASP.


Its an old script being updated for a new world - I wish I could, but I
can´t (as in, it takes too much work...)


I am not sure I buy into that one, but, ok.
I have tried both window.frames[], document.getEle mentById and even
document.getEle mentsByTagName, nothing has worked when the iframe is
involved...
It takes a combination :)


Been there, done that - didn´t work either :(
It seems I cannot get af reference to the table in the iframe from the
parent document... ??
window.frames['IFrameName'].document.getEl ementById('Tabl eID');


No luck...


Then something else in your code is breaking it.
If that doesn't work, post a URL to a sample page that shows the behavior.


Unfortunately not possible at the moment...


Why? Just a sample page. Or, sample code here.
I have a feeling that this ends with a lot of recoding...


It will, eventually, lead to recoding. It is not a matter of if, but when.
This script block:

<script type="text/javascript">
function newScriptElemen t(){
alert(window.fr ames['myIframe'].document.getEl ementById('myTa ble').innerHTML );
}
function addIFrame(){
document.getEle mentById('someC ontainer').inne rHTML =
'<iframe src="blank2.htm l" name="myIframe" ></iframe>';
}
</script>

With this HTML:

<button onclick="addIFr ame()">Add IFrame</button>
<button onclick="newScr iptElement()">G et Table</button>
<div id="someContain er"></div>

I added the IFrame using innerHTML and then got the table innerHTML from
blank2.html:

<table id="myTable">
<tbody>
<tr>
<td>My Table</td>
</tr>
</tbody>
</table>

That leads to the thought that something else in your page is screwing
it up.
--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 1 '06 #4
>That leads to the thought that something else in your page is screwing
it up.


That seems to be the problem :(

I have tried to call a function from the page in the iframe with the
table object as argument and it stille doesn´t work...
doStuffToTable( document.getEle mentById('table thingy');

In a .js file "attached" to the parent page:
function doStuffToTable( t)
{
alert(t.id);
}

That much works just fine...

Then I call a function in the old code that writes to the table, adds
rows and cells, but the table is still empty...
I quess its something in the old code that can't handle DOM working
with the iframe = even more "research" in old confusing code, less
actual work...

May 1 '06 #5
Henrik Stidsen said the following on 5/1/2006 9:31 AM:
That leads to the thought that something else in your page is screwing
it up.


That seems to be the problem :(

I have tried to call a function from the page in the iframe with the
table object as argument and it stille doesn´t work...
doStuffToTable( document.getEle mentById('table thingy');

In a .js file "attached" to the parent page:
function doStuffToTable( t)
{
alert(t.id);
}

That much works just fine...

Then I call a function in the old code that writes to the table, adds
rows and cells, but the table is still empty...


How does it add rows and cells? innerHTML or appendChild? innerHTML
doesn't work real well with IE and in IE you have to appendChild to the
TBODY instead of the Table element. Or, you could use insertRow and
insertCell. But insertRow, in IE, is downright crazy:

<URL:
http://msdn.microsoft. com/library/default.asp?url =/workshop/author/dhtml/reference/methods/insertrow.asp>

Yuck!

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 1 '06 #6
It uses insertRow and insertCell to build the table...

Since yesterday I have found out more about the problem.

The page in the iframe calls a function on the parent page. Argument is
an object, document.getEle mentById('myTab le'), which works fine. From
the parent function I am able to alert the id of the object. So far so
good.
Next step is to change som properties on the table and delete some rows
(actually, changing their ID and setting visibillity to none). After
this, I cannot get myTable via document.getEle mentById so I try this as
a backup:

var tmpTbl = document.getEle mentById('listm ultitable');
if(tmpTbl == null)
{
var iframes = document.getEle mentsByTagName( 'iframe');
var tables;
for(i = 0; i < iframes.length; i++)
{
tables =
iframes[i].document.getEl ementsByTagName ('table');
for(j = 0; j < tables.length; j++)
{
if(tables[j].id == 'listmultitable '){newTbl =
tables[j]; j = tables.lenght; i = iframes.length; }
}
}
}

And well, tmpTbl is still null after this :/

I use the same function at another place in the app where its not in an
iframe and there is no problems at all there :(

May 2 '06 #7
"Henrik Stidsen" <he***********@ gmail.com> skrev i melding
news:11******** **************@ e56g2000cwe.goo glegroups.com.. .
It uses insertRow and insertCell to build the table...

Since yesterday I have found out more about the problem.

The page in the iframe calls a function on the parent page. Argument is
an object, document.getEle mentById('myTab le'), which works fine. From
the parent function I am able to alert the id of the object. So far so
good.
Next step is to change som properties on the table and delete some rows
(actually, changing their ID and setting visibillity to none). After
this, I cannot get myTable via document.getEle mentById so I try this as
a backup:

var tmpTbl = document.getEle mentById('listm ultitable');
if(tmpTbl == null)
{
var iframes = document.getEle mentsByTagName( 'iframe');
var tables;
for(i = 0; i < iframes.length; i++)
{
tables =
iframes[i].document.getEl ementsByTagName ('table');
for(j = 0; j < tables.length; j++)
{
if(tables[j].id == 'listmultitable '){newTbl =
tables[j]; j = tables.lenght; i = iframes.length; }
}
}
}

And well, tmpTbl is still null after this :/

I use the same function at another place in the app where its not in an
iframe and there is no problems at all there :(


Your *backup* method sets 'newTbl', NOT 'tmpTbl'...

Could that be your problem?

--
Dag.
May 2 '06 #8

Dag Sunde wrote:
Your *backup* method sets 'newTbl', NOT 'tmpTbl'... Could that be your problem?


It could have been the problem - but now i've changed it to:
if(tmpTbl == null){ tbl = ntbl; } else { tbl = null; }

tbl being a "global" variable, ntbl being the object passed as argument
to this function. It gets me about 5 centimeters further in the
script...

It seems to me that the problem is that the table is not added to the
DOM tree or the DOM tree that the javascript uses is not updated when I
change the id of the table in the iframe. Could that maybe be the
problem ?
I use the developer toolbar for IE and the DOM tree that it has is
updated correctly.

May 2 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10101
by: Winfried Koenig | last post by:
Hi everyone, I have a main page: -------------------------------------------------- <html><head><title>Test</title> </head><body> <img id="img_a" name="img_a" src="image_1.png" alt=""><br>
2
6687
by: gsb | last post by:
I have an HTML page loaded into an iFrame contained in a DIV tag. From the loaded document how would I reference the DIV object? Thanks, gsb
5
22090
by: Dominic | last post by:
Hi everybody, My goal is to set the height of the iframe to fit its content. There was an earlier posting which gave some useful insights. http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=d2d855ea.0307060828.2965dfda%40posting.google.com&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26q%3DCalculating%2BIFrame%2Bsize The key idea is function getInnerHeight(iframe){
26
45501
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function defined in "A.html", but every attempt results in an "is not a function" error. I have tried to invoke the function using parent.document.funcname(), top.document.funcname(), and various other identifying methods, but all result in the above...
0
1138
by: TR | last post by:
I have two aspx pages. Both pages have the same DOCTYPE: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> On one, there's an IFRAME and it works without no error. On the other, also with an IFRAME, I get the error 'The active schema does not support the element 'iframe'" when in design mode/HTML view. What could be causing this error in one page but not in the other? Thanks!
1
9176
by: gslim | last post by:
I am trying to implement the busybox sample from // From Mark Wagner // http://blogs.crsw.com/mark/articles/642.aspx When I get to this line I get an access denied error. Could someone give me an idea why this might be? Here is the entire function
2
13797
by: Steven | last post by:
I have a page(pg1) which contains a select list (list1) in a form(form1) and an iframe(frame1), in this iframe is a page(pg2) with another select list(list2) in a form(form2) and I transfer the contents of list2 to list 1 as follows function transfer(){ for (var i=0; i<document.form2.list2.length; i++){ var cf=document.form2.list2; addOption(parent.document.form1.list1, document.form2.list2.value, document.form2.list2.value);
1
4650
by: Roy | last post by:
Hi, I have a parent document which has an iframe loaded in it. The iframe has an textfield element. I want to access this textfield element from the parent document. I have tried the following. But that doesn't work. (from the parent) window.frames.document.getElementById('idname')
22
3988
by: thekingsnake | last post by:
After following the instructions from the answer below: http://bytes.com/topic/javascript/answers/153274-script-iframe-can-not-call-functions-defined-parent-document I was able to have the child window perform the function in the parent window. The function was to only have an alert window pop up as the child page was loading. I easily modified this to my intention which was to have a parent function performed when the child window...
0
8406
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8932
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8831
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7449
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2821
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1819
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.