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

Accessing an IFrame's properties

Hi,

I want to access the properties of an IFrame but seem unable to get access
to the IFrames document body.

<html>

<body>
<iframe src="test.html" id="IFrame"></iframe>
</body>

<script>
var f = document.getElementById('IFrame');
f.document.body.innerHTML = "This is another test";
</script>

</html>

This writes to the main document not the IFrame.

I am doing something very basic wrong but cannot work out what. (I do know I
should be doing a open/write/close rather than an innerHTML but this is just
for testing access to the contained IFrames properties)

Help,

Aaron
Apr 5 '06 #1
14 18234
Aaron Gray wrote:
I want to access the properties of an IFrame but seem unable to get access
to the IFrames document body.

<html>

<body>
<iframe src="test.html" id="IFrame"></iframe>
</body>

<script>
var f = document.getElementById('IFrame');
f.document.body.innerHTML = "This is another test";
</script>

</html>

This writes to the main document not the IFrame.
Of course.
I am doing something very basic wrong but cannot work out what.
You are obtaining a reference to an HTMLIFrameElement object, representing
an `iframe' element node in the document tree. By modifying the former's
proprietary `innerHTML' property, you modify the content of the `iframe'
element. This content is displayed only if the `iframe' element is not
supported.
(I do know I should be doing a open/write/close rather than an innerHTML
but this is just for testing access to the contained IFrames properties)


You need to access the document object of the IFrame resource instead.

Proprietary: window.frames['IFrame'].document
(preferably with `name' instead)
<URL:http://research.nihonsoft.org/javascript/ClientReferenceJS13/frame.html>
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_window.asp>

Standards compliant: f.contentDocument
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-50708718>

Similar questions have been asked here before.
PointedEars
Apr 5 '06 #2
> I want to access the properties of an IFrame but seem unable to get access
to the IFrames document body.


Properties are accessable but content is not on IE 6, previous versions
allowed access to the contents.

Firefox allows access to the contents of an IFrame, but IE6 does not.

Aaron
Apr 5 '06 #3
Aaron Gray wrote:
I want to access the properties of an IFrame but seem unable to get
access to the IFrames document body.


Properties are accessable but content is not on IE 6, previous versions
allowed access to the contents.

Firefox allows access to the contents of an IFrame, but IE6 does not.


Nonsense.
PointedEars
Apr 5 '06 #4
>> Properties are accessable but content is not on IE 6, previous versions
allowed access to the contents.

Firefox allows access to the contents of an IFrame, but IE6 does not.


Nonsense.


Try it and please send me some code that disputes this !

I would be most greatful if you can prove me wrong.

Aaron
Apr 5 '06 #5
Aaron Gray wrote:
Properties are accessable but content is not on IE 6, previous versions
allowed access to the contents.

Firefox allows access to the contents of an IFrame, but IE6 does not.


Nonsense.


Try it and please send me some code that disputes this !

I would be most greatful if you can prove me wrong.


<URL:http://pointedears.de/scripts/test/dom/iframe>
PointedEars
Apr 5 '06 #6
>>>> Properties are accessable but content is not on IE 6, previous versions
allowed access to the contents.

Firefox allows access to the contents of an IFrame, but IE6 does not.

Nonsense.


Try it and please send me some code that disputes this !

I would be most greatful if you can prove me wrong.


<URL:http://pointedears.de/scripts/test/dom/iframe>


I seem to be able to read but not modify innerHTML.

I'll modify my statement :-

IFrame content is readable but not modifiable from the parent
window.

Do you agree ?

Thurther :-

<iframe src="document.html" name="IFrame" id="IFrame"></iframe>
<div id="sourceViewDiv"></div>

document.getElementById("sourceViewDiv").innerHTML =
window.frames['IFrame'].document.body.innerHTML;
window.alert(window.frames['IFrame'].document.body.innerHTML);

The first script line fails but the alert works.

Am very confused,

Aaron
Apr 5 '06 #7
Please provide attribution of quoted material, as described in
the Netiquette Guidelines (RFC1855) and the newsgroup's FAQ.[1]
vvvvvvvvvvvvvvvvv
Aaron Gray wrote:
> Properties are accessable but content is not on IE 6, previous
> versions allowed access to the contents.
>
> Firefox allows access to the contents of an IFrame, but IE6 does not.
Nonsense.
Try it and please send me some code that disputes this !

I would be most greatful if you can prove me wrong. <URL:http://pointedears.de/scripts/test/dom/iframe>


I seem to be able to read but not modify innerHTML.


I overlooked that part.
I'll modify my statement :-

IFrame content is readable but not modifiable from the parent
window.

Do you agree ?
That would depend on what you call "IFrame content". Try the URL again
(I modified the document). The first and third button do work for me in
IE 6.0 SP1 [Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)]. All
buttons work in Firefox 1.5.0.1 [Mozilla/5.0 (X11; U; Linux i686; en-US;
rv:1.8.0.1) Gecko/20060313 Debian/1.5.dfsg+1.5.0.1-4 Firefox/1.5.0.1].
Thurther :-

<iframe src="document.html" name="IFrame" id="IFrame"></iframe>
<div id="sourceViewDiv"></div>

document.getElementById("sourceViewDiv").innerHTML =
window.frames['IFrame'].document.body.innerHTML;
It does not appear as if you were attempting to modify the IFrame
document content here.
window.alert(window.frames['IFrame'].document.body.innerHTML);

The first script line fails but the alert works.


ISTM either you have mixed up the sides of the assignment, or you have
described the wrong problem. If the latter, you should make sure that
you are operating on a Valid document first[2], and that
document.getElementById(...) returns an element object reference.[3]
"The first script line fails" is no error report.[4]
PointedEars
___________
[1] <URL:http://jibbering.com/faq/>
[2] <URL:http://validator.w3.org/>
[3] <URL:http://pointedears.de/scripts/test/whatami#inference>
[4] <URL:http://jibbering.com/faq/#FAQ4_43>
Apr 5 '06 #8
> "Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote:
news:85****************@PointedEars.de....
[snip]

That would depend on what you call "IFrame content". Try the URL
again (I modified the document). The first and third button do
work for me in IE 6.0 SP1 [Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.0)]. All buttons work in Firefox 1.5.0.1 [Mozilla/5.0
(X11; U; Linux i686; en-US; rv:1.8.0.1) Gecko/20060313
Debian/1.5.dfsg+1.5.0.1-4 Firefox/1.5.0.1].

[snip]
How would it work in all them browsers if you replace contentDocument
with contentWindow.document?

--
BootNic Wednesday, April 05, 2006 4:15 PM

It's not that some people have willpower and some don't. It's that
some people are ready to change and others are not.
*James Gordon*

Apr 5 '06 #9
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:85****************@PointedEars.de...
Please provide attribution of quoted material, as described in
the Netiquette Guidelines (RFC1855) and the newsgroup's FAQ.[1]
vvvvvvvvvvvvvvvvv
Aaron Gray wrote:
>> Properties are accessable but content is not on IE 6, previous
>> versions allowed access to the contents.
>>
>> Firefox allows access to the contents of an IFrame, but IE6 does not.
> Nonsense.
Try it and please send me some code that disputes this !

I would be most greatful if you can prove me wrong.
<URL:http://pointedears.de/scripts/test/dom/iframe>


I seem to be able to read but not modify innerHTML.


I overlooked that part.
I'll modify my statement :-

IFrame content is readable but not modifiable from the parent
window.

Do you agree ?


That would depend on what you call "IFrame content". Try the URL again
(I modified the document). The first and third button do work for me in
IE 6.0 SP1 [Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)]. All
buttons work in Firefox 1.5.0.1 [Mozilla/5.0 (X11; U; Linux i686; en-US;
rv:1.8.0.1) Gecko/20060313 Debian/1.5.dfsg+1.5.0.1-4 Firefox/1.5.0.1].
Thurther :-

<iframe src="document.html" name="IFrame" id="IFrame"></iframe>
<div id="sourceViewDiv"></div>

document.getElementById("sourceViewDiv").innerHTML =
window.frames['IFrame'].document.body.innerHTML;


It does not appear as if you were attempting to modify the IFrame
document content here.


No just reading its text into a div.
window.alert(window.frames['IFrame'].document.body.innerHTML);

The first script line fails but the alert works.


ISTM either you have mixed up the sides of the assignment, or you have
described the wrong problem. If the latter, you should make sure that
you are operating on a Valid document first[2], and that
document.getElementById(...) returns an element object reference.[3]
"The first script line fails" is no error report.[4]


It was returning null.

Your example shows the modification of IFrame innerHTML working.

Looks like it is a timing error in my code. Doing mods explicitly to the
IFrame on loading the parent document causes a 'null' value for that
documents body.

Many thanks for your help Thomas,

Aaron

Apr 5 '06 #10
Seeing as window.frames isn't a part of any w3c standard, surely there
must be another (standards-compliant) way to access the properties of a
document included using the iframe or object elements (preferably
object)... right?

*** Sent via Developersdex http://www.developersdex.com ***
Apr 30 '06 #11
Declan Naughton <pi***********@gmail.com> writes:
Seeing as window.frames isn't a part of any w3c standard, surely there
must be another (standards-compliant) way to access the properties of a
document included using the iframe or object elements (preferably
object)... right?


Yes.
---
var iframeElem = document.getElementById("myIframeId");
var iframeDoc = iframeElem.contentDocument;
iframeDoc.body.style.backgroundColor="red"; // i'm in!
---

Does it work in IE? Ofcourse not.

In IE, iframe elements does not have a "contentDocument" property.
They do have a "contentWindow", so you can do:
---
var iframeDoc = iframeElem.contentDocument ||
(iframeElem.contentWindow && iframeElem.contentWindow.document);
---

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 30 '06 #12
Nice, that works for objects too.

Thanks!

*** Sent via Developersdex http://www.developersdex.com ***
May 1 '06 #13
Declan Naughton wrote:
Seeing as window.frames isn't a part of any w3c standard, surely there
must be another (standards-compliant) way to access the properties of a
document included using the iframe or object elements (preferably
object)... right?


Like many things, standards compliance is not an absolute on the Web. In
fact, there is no fully standards-compliant approach to the DOM as the W3C
DOM standards define only interfaces that can be implemented. However, the
implementation always is proprietary; so is also `document' or
`window.document' to implement _also_ the HTMLDocument interface of W3C DOM
Level 2 HTML. One could only argue that

document.getElementById("myIframeId").contentDocum ent

as Lasse suggested, is "more standards-compliant" than

window.frames[myIframeId].contentDocument

because the proprietary part of the former is "smaller". But I doubt this
holds water. However, knowing this, I also tend to call something
"standards compliant" if only the implementation is the proprietary part of
the reference.

Trying to achieve maximum standards compliance can have its drawbacks. For
example, usually you want to give your IFrame a name instead of an ID
because UAs want the `name' attribute value for the `target' property
value of a[href]s. Trying to be standards compliant here would mean

document.getElementsByName("myIframeName")[0].contentDocument

(not as this reference worm, of course) as compared to

window.frames[myIframeName].contentDocument

Certainly I would almost always prefer the latter here as first
alternative in a collection of dependent feature tests; it has
broader support, and is more efficient. Hopefully, the Web API
Specification will make that quasi-standard a specified standard
eventually.
Please quote the minimum of what you are replying to --

<URL:http://jibbering.com/faq/faq_notes/pots1.html>
<URL:http://www.safalra.com/special/googlegroupsreply/>

-- and don't use buggy Web interfaces to NetNews such as Developersdex's
AspNNTP. Google Groups' interface is slightly better than that (at least
it supports threads properly and with a few tweaks also automatic
quotation), but a newsreader program is best.
PointedEars
--
#define QUESTION ((bb) || !(bb))
// William Shakespeare (if he would have been a hacker ;-))
May 2 '06 #14
Declan Naughton wrote:
Seeing as window.frames isn't a part of any w3c standard, surely there
must be another (standards-compliant) way to access the properties of a
document included using the iframe or object elements (preferably
object)... right?


Like many things, standards compliance is not an absolute on the Web. In
fact, there is no fully standards-compliant approach to the DOM as the W3C
DOM standards define only interfaces that can be implemented. However, the
implementation always is proprietary; so is also `document' or
`window.document' to implement _also_ the HTMLDocument interface of W3C DOM
Level 2 HTML. One could only argue that

document.getElementById("myIframeId").contentDocum ent

as Lasse suggested, is "more standards-compliant" than

window.frames[myIframeId].contentDocument

because the proprietary part of the former is "smaller". But I doubt this
holds water. However, knowing this, I also tend to call something
"standards compliant" if only the implementation is the proprietary part of
the reference.

Trying to achieve maximum standards compliance can have its drawbacks. For
example, usually you want to give your IFrame a name instead of an ID
because UAs want the `name' attribute value for the `target' attribute
value of a[href]s. Trying to be standards compliant here would mean

document.getElementsByName("myIframeName")[0].contentDocument

(not as this reference worm, of course) as compared to

window.frames[myIframeName].contentDocument

Certainly I would almost always prefer the latter here as first
alternative in a collection of dependent feature tests; it has
broader support, and is more efficient. Hopefully, the Web API
Specification will make that quasi-standard a specified standard
eventually.
Please quote the minimum of what you are replying to --

<URL:http://jibbering.com/faq/faq_notes/pots1.html>
<URL:http://www.safalra.com/special/googlegroupsreply/>

-- and don't use buggy Web interfaces to NetNews such as Developersdex's
AspNNTP. Google Groups' interface is slightly better than that (at least
it supports threads properly and with a few tweaks also automatic
quotation), but a newsreader program is best.
PointedEars
--
#define QUESTION ((bb) || !(bb))
// William Shakespeare (if he would have been a hacker ;-))
May 2 '06 #15

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

Similar topics

1
by: Griff | last post by:
I am trying to write a program in VB6 to get at file properties (Owner, Title, etc ). I can access them using scripting, Set objShell = CreateObject("Shell.Application") Set objFolder =...
0
by: TB | last post by:
(re-wording a previous post.. this makes much more sense, sorry for any duplication) First off, I've got three classes: Class MyItem Public PropertyOne As String Public PropertyTwo As String...
1
by: GautamKumar | last post by:
I want to access(SET) all the properties of IIS programmatically using C# I need to set the IIS properties through the code.plzz let me know wat class we have to use to access these properties or...
2
by: chauncy | last post by:
Hi, I have a databound dropdown filled with data. The text value is a file path that I want to use to open that file in a new browser window. I can do this with a html dropdown because I can...
1
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named...
3
by: BillGatesFan | last post by:
Alright, this is very strange. I call a javascript function in my page load that accesses an iframe and I get an error. But if I wait till the page completely loads and access the iframe through a...
0
by: Bishop | last post by:
I've figured out how to create a custom TreeNode with custom properties and I can view those properties at runtime, but I'm not sure how to change them after I've added the custom node to the tree....
4
by: evantay | last post by:
I'm using ASP.NET 2.0 with VS.NET 2005. I'm trying to access properties from my master pages within a page that inherits from that master page (a child page). However the values are always null....
0
by: Matt Nunnally | last post by:
Hi, I have a DLL I created in .NET. I have certain exposed properties that I have exposed that are declared as Integer or Byte arrays. Trying to access those certain properties through unmanaged...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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?
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...

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.