object "has no properties", for the millionth time...

G

GfxGuy

I've seen this problem posted a million times, but I've read through
all of them and can't figure out what I'm doing wrong.

Simple example (this is the whole file, no editing):

----------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<style type="text/css">
#msg {
width: 512;
z-index: 10;
background-color: #bbbbbb;
text-align: left;
}
</style>

<script type="text/javascript">
<!--
function init()
{
var obj = document.getElementById("msg");
obj.document.open();
obj.document.write("Initialized");
obj.document.close();
}

-->
</script>
</head>

<body onload="init()">
<div id="msg">Info here</div>
</body>

</html>

----------

The page displays correctly, the background of the div is gray, it
shows "Info here", but never gets changed to "Initialized". I've done
a few other things that I removed to verify that init was being called,
and called at the right time, but I've even thrown in a mouseover to
make sure that it wasn't because the document wasn't loaded, and got
the same error.

On the obj.document.open() line, I get "obj.document has no properties"
in Firefox. In IE I get a warning that the page can't be displayed
correctly... the view source in IE actually shows nothing.

What's nice is firefox can replace those three lines with just

obj.textContent="Initialized";

But IE doesn't seem to like that.

Come on, it's got to be something REALLY simple... I've tried single
quotes, double quotes, having the div section written by a
document.write...

TIA,
Fred
 
M

Martin Honnen

GfxGuy wrote:

function init()
{
var obj = document.getElementById("msg");
obj.document.open();
obj.document.write("Initialized");
obj.document.close();
<div id="msg">Info here</div>

I don't know where you got the impression that an element object has a
document property and that it makes sense to call document.write on
that, that only makes sense in Netscape 4 with a positioned div or a
layer but nowhere else
Thus while
var obj = document.getElementById("msg");
in IE 5 and later, Netscape 6 and later, Opera 7 and later gives you the
<div> element object which you could manipulate with W3C DOM methods it
does not make sense then to access
obj.document
and try write to it.
If you want to access the document an element object belongs to then the
corret W3C DOM way is
obj.ownerDocument
What's nice is firefox can replace those three lines with just

obj.textContent="Initialized";

But IE doesn't seem to like that.

So you want to set the content of the <div>? Then in a cross browser way
you can use
obj.innerHTML = ...
while there are other more browser specific ways e.g.
obj.innerText = ...
for IE and Opera or there is the W3C DOM stuff which can remove and
insert nodes.
 
G

GfxGuy

I don't know where you got the impression that an element object has
a document property and that it makes sense to call document.write on
that, that only makes sense in Netscape 4 with a positioned div or a
layer but nowhere else

I got the impression from "JAVASCRIPT in easy steps" by Mike McGrath,
page 164 on "Dynamic Content".

Two things I did differently: I put the div in the html directly
instead of using a document.write("<div id= blah blah blah...")...

AND I didn't give it an exact position...

So the answer to my problem is that what I tried to do only works on an
object with an absolute, given position? And you are saying it's the
"old" way to do it? The book is (c) 2003... So what you suggest
worked great (innerHTML), which I'd seen using the DOM inspector in
Firefox, but is that "old" too?
 
M

Markus Fischer

GfxGuy said:
a document property and that it makes sense to call document.write on
that, that only makes sense in Netscape 4 with a positioned div or a
layer but nowhere else

I got the impression from "JAVASCRIPT in easy steps" by Mike McGrath,
page 164 on "Dynamic Content".

Two things I did differently: I put the div in the html directly
instead of using a document.write("<div id= blah blah blah...")...

So was he using to write the content of the div directly with this
document.write method?
AND I didn't give it an exact position...

So the answer to my problem is that what I tried to do only works on an
object with an absolute, given position? And you are saying it's the
"old" way to do it? The book is (c) 2003... So what you suggest
worked great (innerHTML), which I'd seen using the DOM inspector in
Firefox, but is that "old" too?

It doesn't matter whether you position your DIV or not. 'document'
refers to the whole HTML document and the write method inserts the
passed string into the document.

write is only a method of the document itself, not of any of the objects
inside the document. If you want to change the content of an element in
you're page you usually use innerHTML like you have been told oder you
use document.createTextNode and add it as a children to element you want.

e.g.

var msg = document.getElementById('msg');
msg.appendChild(document.createTextNode('Initialized'));

HTH
 
G

GfxGuy

With all due respect to Mr. McGrath's copyright (comments ommited for
brevity):

document.write("<div id='lyr'
style="position:absolute;top:90px;left:10px;width:200px;height:10px;z-index:10;background-color:eek:range'>
</div>");

function write_entry()
{
var str = document.forms.f.txt.value;
var obj = document.getElementById("lyr");
obj.document.open();
obj.document.write(str);
obj.document.close();
}
---

Obviously "f" is a form element with a text input called "txt".

But I just went to the website that supports the book and in the source
code you can download, without mentioning any errata, they have in fact
changed the code to obj.innerHTML = str;

Thanks for your help.
 
T

Thomas 'PointedEars' Lahn

^^^^^^^^^^^^^^^^^^^^
Great core dump! Throw it away, NOW.
It doesn't matter whether you position your DIV or not.

It does matter in the NS4 DOM as only positioned elements are part of the
`document.layers' collection there. However, as already stated, that is
a completely different DOM than actually used, it simply does not apply
here.


PointedEars
 
J

jponiato

GfxGuy said:
With all due respect to Mr. McGrath's copyright (comments ommited for
brevity):

document.write("<div id='lyr'
style="position:absolute;top:90px;left:10px;width:200px;height:10px;z-index:
10;background-color:eek:range'>
</div>");

function write_entry()
{
var str = document.forms.f.txt.value;
var obj = document.getElementById("lyr");
obj.document.open();
obj.document.write(str);
obj.document.close();
}
---

Obviously "f" is a form element with a text input called "txt".

But I just went to the website that supports the book and in the source
code you can download, without mentioning any errata, they have in fact
changed the code to obj.innerHTML = str;

Thanks for your help.

I, too, have been struggling with the exact same code from the same source.
Thanks for letting us know about the update. Too bad they don't list it in
their 'updates' section of the web site.

Jp
 
G

GfxGuy

Serves me right for not sticking with O'Reilly. I always end up
dissappointed with non-O'Reilly books. Buy a cheap book, you get what
you pay for.
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top