467,915 Members | 1,552 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

DHTML question: DOM access to <OBJECT type="text/html" data="..." />

I recently learned how to do an <OBJECT> alternative to <IFRAME> in current
browsers using:

<object id="extendedhtml" type="text/html" data="otherpage.html" width="250"
height="400"></object>

My question is how do I access the document DOM of this object in
Javascript? For example, "alert(extendedhtml.innerHTML);" doesn't work and
produces an unknown error. I'd like to both read and write to the document's
body element's innerHTML property, as well as access its full DOM (forms,
etc) as I would access the elements of the main page.

Thanks,
Jon

PS Please do not tell me about server-side includes, suggest
document.writes, or belabor <IFRAME>. I'm just asking about document-loading
<OBJECT> tags, if anyone knows. Thanks.
Jul 23 '05 #1
  • viewed: 4842
Share:
6 Replies


Jon Davis wrote:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current
browsers using:

<object id="extendedhtml" type="text/html" data="otherpage.html" width="250"
height="400"></object>

My question is how do I access the document DOM of this object in
Javascript? For example, "alert(extendedhtml.innerHTML);" doesn't work and
produces an unknown error. I'd like to both read and write to the document's
body element's innerHTML property, as well as access its full DOM (forms,
etc) as I would access the elements of the main page.


The W3C DOM is documented here:
http://www.w3.org/TR/DOM-Level-2-HTML/
with the HTMLObjectElement being documented here:
http://www.w3.org/TR/DOM-Level-2-HTM...tml#ID-9893177
so there you will see that according to that specification there is a
property
contentDocument
meaning in browsers implementing that specification you can acess
document.getElementById('extendedhtml').contentDoc ument
e.g. if you really want to access the innerHTML of the <body> element of
the document loaded in the <object>
var objDoc = document.getElementById('extendedhtml').contentDoc ument;
... objDoc.body.innerHTML ...
Only problem is that MSIE doesn't know contentDocument at all, its
documentation of <object> element is here
http://msdn.microsoft.com/library/de...cts/object.asp
It seems however that MSIE exposes the document inside of the <object>
via the property
object
so the code to work with MSIE and with DOM Level 2 browsers like Mozilla
or recent Opera 7 versions is:

<html lang="en">
<head>
<title>object scripting in MSIE and in DOM Level 2 HTML browsers</title>
<script type="text/javascript">
window.onload = function (evt) {
var obj;
if (document.getElementById && (obj =
document.getElementById('htmlObject1'))) {
var objDoc;
if (obj.contentDocument) {
objDoc = obj.contentDocument;
}
else if (obj.object) {
objDoc = obj.object;
}
if (objDoc && objDoc.body) {
alert(objDoc.body.innerHTML);
}
}
};
</script>
</head>
<body>
<p>
<object id="htmlObject1"
type="text/html"
data="jsInterpreter.html"
width="100%"
height="300"></object>
</p>
</body>
</html>

Crosspost and followup-to to comp.lang.javascript.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Jon Davis wrote:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current
browsers using:

<object id="extendedhtml" type="text/html" data="otherpage.html" width="250"
height="400"></object>

My question is how do I access the document DOM of this object in
Javascript?


Browser support for <object type="text/html" ...> is very poor. Are you
absoluty sure you want to switch from iframe to this ?
Jul 23 '05 #3
Pierre Goiffon <pg******@invalid.fr> wrote:
Browser support for <object type="text/html" ...> is very poor.


<panto>Oh no it isn't!</panto>

Supply width and height as attribute values and even IE supports it.

--
Spartanicus
Jul 23 '05 #4
Jon Davis wrote:
My question is how do I access the document DOM of this object
[snip]
PS Please do not tell me about server-side includes, suggest
document.writes, or belabor <IFRAME>.


"Get real! This is not a helpdesk, it's a discussion forum." etc.

--
Brian (remove "invalid" to email me)
Jul 23 '05 #5

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:41***********************@newsread2.arcor-online.net...


Jon Davis wrote:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current browsers using:

<object id="extendedhtml" type="text/html" data="otherpage.html" width="250" height="400"></object>

My question is how do I access the document DOM of this object in
Javascript? For example, "alert(extendedhtml.innerHTML);" doesn't work and produces an unknown error. I'd like to both read and write to the document's body element's innerHTML property, as well as access its full DOM (forms, etc) as I would access the elements of the main page.
The W3C DOM is documented here:
http://www.w3.org/TR/DOM-Level-2-HTML/
with the HTMLObjectElement being documented here:
http://www.w3.org/TR/DOM-Level-2-HTM...tml#ID-9893177
so there you will see that according to that specification there is a
property
contentDocument
meaning in browsers implementing that specification you can acess
document.getElementById('extendedhtml').contentDoc ument
e.g. if you really want to access the innerHTML of the <body> element of
the document loaded in the <object>
var objDoc = document.getElementById('extendedhtml').contentDoc ument;
... objDoc.body.innerHTML ...
Only problem is that MSIE doesn't know contentDocument at all, its
documentation of <object> element is here

http://msdn.microsoft.com/library/de...cts/object.asp It seems however that MSIE exposes the document inside of the <object>
via the property
object
so the code to work with MSIE and with DOM Level 2 browsers like Mozilla
or recent Opera 7 versions is:

<html lang="en">
<head>
<title>object scripting in MSIE and in DOM Level 2 HTML browsers</title>
<script type="text/javascript">
window.onload = function (evt) {
var obj;
if (document.getElementById && (obj =
document.getElementById('htmlObject1'))) {
var objDoc;
if (obj.contentDocument) {
objDoc = obj.contentDocument;
}
else if (obj.object) {
objDoc = obj.object;
}
if (objDoc && objDoc.body) {
alert(objDoc.body.innerHTML);
}
}
};
</script>
</head>
<body>
<p>
<object id="htmlObject1"
type="text/html"
data="jsInterpreter.html"
width="100%"
height="300"></object>
</p>
</body>
</html>

Crosspost and followup-to to comp.lang.javascript.

--

Martin Honnen
http://JavaScript.FAQTs.com/


Thank you, Martin.

I'm so sick of everyone else whining about how "this isn't a help desk",
etc. Martin, if you are interested, please e-mail me at
jo*@REMOVE.ME.powerblog.net for a free copy of PowerBlog if you want it.
Thanks for your help.

Cheers,
Jon Davis

PS This is an HTML DOM issue, not a JavaScript issue. JavaScript is not
necessarily browser-specific, and DOM is not necessarily related to
Javascript (in Internet Explorer, all ActiveX scripting languages apply,
including VBScript). You proved this by pointing to a W3C HTML DOM document
URL rather than a Javascript standard (ECMAScript) document URL. So I will
be posting here, not there, thank you.
Jul 23 '05 #6
On Sat, 18 Dec 2004 03:55:14 -0800, Jon Davis <jo*@REMOVE.ME.jondavis.net>
wrote:

[snip]
PS This is an HTML DOM issue, not a JavaScript issue. [...] So I will be
posting here, not there, thank you.


The comp.lang.javascript group covers various issues, not limited to
ECMAScript (and its derivatives). Browser DOMs are by far the most
frequently discussed topic. Scripting questions should be directed there,
or possibly alt.html or alt.html.dhtml.

As for it being on topic here in ciwah: you are wrong. As the name of the
group implies, questions should relate to authoring HTML for the Web
(<URL:http://www.stack.nl/~boris/HTML/ciwahfaq.html#p1>).

Mike
Please trim your quotations.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Arash Dejkam | last post: by
2 posts views Thread by Warren Tang | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.