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

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 someContainerElement.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.getElementById and even
document.getElementsByTagName, 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 52796
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 someContainerElement.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.getElementById and even
document.getElementsByTagName, 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.getElementById('TableID');

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

--
Randy
comp.lang.javascript 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.getElementById and even
document.getElementsByTagName, 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.getElementById('TableID');
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.getElementById and even
document.getElementsByTagName, 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.getElementById('TableID');


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 newScriptElement(){
alert(window.frames['myIframe'].document.getElementById('myTable').innerHTML);
}
function addIFrame(){
document.getElementById('someContainer').innerHTML =
'<iframe src="blank2.html" name="myIframe"></iframe>';
}
</script>

With this HTML:

<button onclick="addIFrame()">Add IFrame</button>
<button onclick="newScriptElement()">Get Table</button>
<div id="someContainer"></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.javascript 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.getElementById('tablething y');

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.getElementById('tablething y');

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.javascript 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.getElementById('myTable'), 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.getElementById so I try this as
a backup:

var tmpTbl = document.getElementById('listmultitable');
if(tmpTbl == null)
{
var iframes = document.getElementsByTagName('iframe');
var tables;
for(i = 0; i < iframes.length; i++)
{
tables =
iframes[i].document.getElementsByTagName('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.googlegr oups.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.getElementById('myTable'), 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.getElementById so I try this as
a backup:

var tmpTbl = document.getElementById('listmultitable');
if(tmpTbl == null)
{
var iframes = document.getElementsByTagName('iframe');
var tables;
for(i = 0; i < iframes.length; i++)
{
tables =
iframes[i].document.getElementsByTagName('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
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
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
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. ...
26
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...
0
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,...
1
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...
2
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...
1
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....
22
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...
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
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
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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.