Writing to an IFRAME's innerHTML

A

Aaron Gray

How do I set the innerHTML property of a contained IFRAME ?

Many thanks in advance,

Aaron
 
A

Aaron Gray

Aaron Gray said:
How do I set the innerHTML property of a contained IFRAME ?

I got

window.frames["results"].document.body.innerHTML

This works on FF and Opera, but not on IE.

Aaron
 
J

Joost Diepenmaat

Aaron Gray said:
Aaron Gray said:
How do I set the innerHTML property of a contained IFRAME ?

I got

window.frames["results"].document.body.innerHTML

This works on FF and Opera, but not on IE.

IIRC, window.frames["results"].document.contentWindow.document.body.innerHTML

should work on IE.
 
A

Aaron Gray

Joost Diepenmaat said:
Aaron Gray said:
Aaron Gray said:
How do I set the innerHTML property of a contained IFRAME ?

I got

window.frames["results"].document.body.innerHTML

This works on FF and Opera, but not on IE.

IIRC,
window.frames["results"].document.contentWindow.document.body.innerHTML

should work on IE.

window.frames["result"].document.contentWindow.document

Is saying the above is null or not an object.

I have an IFRAME thus :-

<iframe id="result" name="result"></iframe>

Confused, they sure do make this stuff easy !

Aaron
 
J

Joost Diepenmaat

Aaron Gray said:
Joost Diepenmaat said:
Aaron Gray said:
How do I set the innerHTML property of a contained IFRAME ?

I got

window.frames["results"].document.body.innerHTML

This works on FF and Opera, but not on IE.

IIRC,
window.frames["results"].document.contentWindow.document.body.innerHTML

should work on IE.

window.frames["result"].document.contentWindow.document

Is saying the above is null or not an object.

http://msdn.microsoft.com/en-us/library/ms533692(VS.85).aspx

You may want to leave out the first "document".
Confused, they sure do make this stuff easy !

Of course they don't.
 
A

Aaron Gray

Joost Diepenmaat said:
Aaron Gray said:
Joost Diepenmaat said:
How do I set the innerHTML property of a contained IFRAME ?

I got

window.frames["results"].document.body.innerHTML

This works on FF and Opera, but not on IE.


IIRC,
window.frames["results"].document.contentWindow.document.body.innerHTML

should work on IE.

window.frames["result"].document.contentWindow.document

Is saying the above is null or not an object.

http://msdn.microsoft.com/en-us/library/ms533692(VS.85).aspx

Wow, great documentation, looks really old stuff.
You may want to leave out the first "document".

Tried that :-

window.frames.result.contentWindow.document is null or not an object

Again
Of course they don't.

Cant find any good documentation on it, tried W3C standard approach too.

Aaron
 
J

Joost Diepenmaat

Aaron Gray said:
window.frames.result.contentWindow.document is null or not an object

In the posts above you refer to

window.frames.results

with an S at the end.
 
A

Aaron Gray

Joost Diepenmaat said:
In the posts above you refer to

window.frames.results

with an S at the end.

Yes, its not that, sorry to confuse.

I am wondering whether it was banned as a secuity problem.

Aaron
 
D

Doug Gunnoe

Yes, its not that, sorry to confuse.

I am wondering whether it was banned as a secuity problem.

Aaron

If the document in the iframe is on another site it will throw an
exception. Security issue.
If you catch the exception and show it in an alert box, it will say
something like:
"Error: Permission denied to get property Window.document"

I have recently accessed an iframe like so:

frames['name_of_the_frame'].document

here is another article on the topic:
http://xkr.us/articles/dom/iframe-document/

As to the innerHTML, can I ask why you want this? I'm pretty certain
that if you are trying to manipulate the document in the iframe, you
don't want the innerHTML.

Here is where I recently piddled around with iframes:
http://polisick.com/crossdomainformattempt.php
 
A

Aaron Gray

Doug Gunnoe said:
here is another article on the topic:
http://xkr.us/articles/dom/iframe-document/
-
Thats great, it works when on a button or on body onload, but not executed
as inline javascript.

That was my problem !
As to the innerHTML, can I ask why you want this? I'm pretty certain
that if you are trying to manipulate the document in the iframe, you
don't want the innerHTML.

For test purposes when testing AJAX.

Thanks that helped alot !:)

Aaron
 
A

Aaron Gray

Its now a two liner :-

//
// getFrameDoc() - get the document for a frame window
//
// Note: this must be executed via body.onload or later
//

function getFrameDoc( frame)
{
var doc = (frame.contentWindow || frame.contentDocument);
return doc.document || doc
}

Aaron
 
A

Aaron Gray

Aaron Gray said:
Its now a two liner :-

//
// getFrameDoc() - get the document for a frame window
//
// Note: this must be executed via body.onload or later
//

function getFrameDoc( frame)
{
var doc = (frame.contentWindow || frame.contentDocument);
return doc.document || doc
}

I have also found this will only work for a frame got by id, not from
window.frames[] :-

var framedoc = getFrameDoc( document.getElementById('myframe'));

works, where :-

var framedoc = getFrameDoc( window.frames["myframe"])

throws an error.

Anyone throw some light on this ?

Thanks,

Aaron
 
T

Thomas 'PointedEars' Lahn

Aaron said:
function getFrameDoc( frame)
{
var doc = (frame.contentWindow || frame.contentDocument);
return doc.document || doc
}

I have also found this will only work for a frame got by id, not from
window.frames[] :-

var framedoc = getFrameDoc( document.getElementById('myframe'));

works, where :-

var framedoc = getFrameDoc( window.frames["myframe"])

throws an error.

You could be a bit more specific. Which error, on which line, in which
runtime environment?


PointedEars

P.S.: Firebug 1.2.0 (final) has been released! :)
 
A

Aaron Gray

Thomas 'PointedEars' Lahn said:
Aaron said:
"Aaron Gray" <[email protected]> wrote [...]:

Please do not write attribution novels, see <http://insideoe.com/>
(Windows Mail is Outlook Express's successor).

I do not understand please elaborate.
function getFrameDoc( frame)
{
var doc = (frame.contentWindow || frame.contentDocument);
return doc.document || doc
}

I have also found this will only work for a frame got by id, not from
window.frames[] :-

var framedoc = getFrameDoc( document.getElementById('myframe'));

works, where :-

var framedoc = getFrameDoc( window.frames["myframe"])

throws an error.

You could be a bit more specific. Which error, on which line, in which
runtime environment?

IE7 the second line.

By Id :-

http://www.aarongray.org/Test/JavaScript/IFrame-test2.html

By window.frames[]

http://www.aarongray.org/Test/JavaScript/IFrame-test3.html

Its probably something very simple I am not familuar with yet.

Aaron
 
L

Lasse Reichstein Nielsen

Aaron Gray said:
var framedoc = getFrameDoc( document.getElementById('myframe'));

works, where :-

var framedoc = getFrameDoc( window.frames["myframe"])

throws an error.

Without having read the rest of the thread, so I don't know what
getFrameDoc does, the important difference is likely to be that
document.getElementById('myframe')
returns the "iframe" element that is part of this document's DOM
tree, whereas
window.frames["myframe"]
evaluates to the window object of the page that is the content
of the frame.
I.e., one is a DOM element object in the scope of the current page,
the other is the window object of the nested page. They are quite
different objects. You probably want the latter.

/L
-
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
S

SAM

Aaron Gray a écrit :
Tried that :-

window.frames.result.contentWindow.document is null or not an object

for sure : there is NO document in your iframe !
(no src)

does : window.frames.results
is same as : window.frames['results']
or : document.results
or : parent.results

Try with an html file (with empty body?) opened in your iframe

or/and (only because you've a name in your iframe) perhaps :
parent.results.contentWindow.document.body.innerHTML
(not tested)

If in first step you open a file (*of same domain*) in your iframe,
probably you'll have no more problem of security.
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Aaron Gray said:
var framedoc = getFrameDoc( document.getElementById('myframe'));

works, where :-

var framedoc = getFrameDoc( window.frames["myframe"])

throws an error.

Without having read the rest of the thread, so I don't know what
getFrameDoc does, [...]

the declaration was right above that, in the part that you snipped :)


Regards,

PointedEars

P.S.:
<***@hotpop.com>: host mx1.hotpop.com[38.113.3.81] said:
550 <***@hotpop.com>:
Recipient address rejected: Account closed due to inactivity.
No forwarding information is available. (in reply to RCPT TO command)

And your signature delimiter is borken; must be "--<SP><CR><LF>":
 
S

SAM

Aaron Gray a écrit :
var framedoc = getFrameDoc( window.frames["myframe"])

throws an error.

Probably better with (if named iframe) :

var framedoc = getFrameDoc( parent.myframe );

or :

var framedoc = getFrameDoc( top.myframe );

Perhaps :
var framedoc = getFrameDoc( window.frames["myframe"])


Anyway it is much better to use ID
(with name that will no more work if the main page is itself in a frame)


Here is a code working in my Firefox 3 and IE 6:
<http://cjoint.com/data/iBmXw4PQVM_ecrit_frame.htm>

Extracts :

<script type="text/javascript">
function scrib(frameNameOrId, texto) {
var f = document.getElementById(frameNameOrId)?
document.getElementById(frameNameOrId) :
top[frameNameOrId];
f = (f.contentWindow || f.contentDocument || f);
f = f.document || f;
f.body.innerHTML = texto;
}
</script>

<iframe name="results" border=1></iframe>
<iframe id="result" border=1></iframe>
<p><a href="javascript:scrib('results','hello world')">test by name</a>
<p><a href="javascript:scrib('result','hello world')">test by id</a>
 
O

optimistx

Thomas said:
And your signature delimiter is borken; must be "--<SP><CR><LF>":

Would somebody explain me the word 'borken'? Google gives this answer in
english to the query define:borken

Borken is a Kreis (district) in the northwestern part of North
Rhine-Westphalia, Germany. Neighboring districts are Bentheim, Steinfurt,
Coesfeld ...
en.wikipedia.org/wiki/Borken (district)

I would otherwise think this means 'broken', but when it has been used at
least tens of times by a person, who admirably thoroughly corrects every
detail, then there is obviously a good reason to use this word 'borken'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,812
Members
47,357
Latest member
sitele8746

Latest Threads

Top