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

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

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

Jul 23 '05 #1
7 5821


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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
> 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?

Jul 23 '05 #3
GfxGuy wrote:
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...")...


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('Initializ ed'));

HTH
Jul 23 '05 #4
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:orange'>
</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.

Jul 23 '05 #5
Markus Fischer wrote:
GfxGuy wrote:
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... ^^^^^^^^^^^^^^^^^^^^
Great core dump! Throw it away, NOW.
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.


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
Jul 23 '05 #6

"GfxGuy" <gf****@mad.scientist.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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:orange'> </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
Jul 23 '05 #7
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.

Jul 23 '05 #8

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

Similar topics

3
by: python newbie | last post by:
Hi, I have a wxPython app which dump errors when I close it ( in the debug output at bottom of Komodo, when I close my app. ) Where I got the code for my GUI: Straight from the wxProject.py...
1
by: Kevin | last post by:
Help, I am running a pass through query to oracle from SQL server 2000 as follows; select * from openquery(nbsp, 'select * from FND_FLEX_VALUES') I have run this query through both DTS and...
7
by: Danny Tuppeny | last post by:
Hi All, I've been going through Google Groups, but nothing seems to quite match my problem. It's hard to post a WSDL or anything at the moment (it's not my service, I'm just using it), but maybe...
2
by: christopher.secord | last post by:
I'm having a hard time understanding an error that I'm getting. In the code below, I'm trying to call substring() on an attribute of an anchor. The error I get says "getAttribute("rel") has no...
4
by: dr1ft3r | last post by:
Hey guys, I'm building a site for a landscaping business down the street and can't seem to get part of the code functioning correctly. The code fails on line 68 where I make a reference to an...
1
by: luispunchy | last post by:
I have an accordion style dropdown list/sublist menu (functions similar to the "today on WebMD video" widget found on http://www.webmd.com/) - it will allow users to click on a headline (from the...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
6
by: maminx | last post by:
hello all, i have this html below <input type="text" name="items" size="3" value="1" onchange="javascript:showNameObject();"/> and i want to alert that object of name with this script ...
0
by: Matt Nordhoff | last post by:
Barak, Ron wrote: For some reason, when you run "import locale", it's importing wx.locale instead of the "locale" module from the stdlib. However, I have no idea why that would be happening......
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.