473,573 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById("msg") ;
obj.document.op en();
obj.document.wr ite("Initialize d");
obj.document.cl ose();
}

-->
</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 "Initialize d". 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.op en() line, I get "obj.docume nt 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 ="Initialize d";

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 5838


GfxGuy wrote:

function init()
{
var obj = document.getEle mentById("msg") ;
obj.document.op en();
obj.document.wr ite("Initialize d");
obj.document.cl ose(); <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.getEle mentById("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.ownerDocume nt
What's nice is firefox can replace those three lines with just

obj.textContent ="Initialize d";

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.create TextNode and add it as a children to element you want.

e.g.

var msg = document.getEle mentById('msg') ;
msg.appendChild (document.creat eTextNode('Init ialized'));

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:9 0px;left:10px;w idth:200px;heig ht:10px;z-index:10;backgr ound-color:orange'>
</div>");

function write_entry()
{
var str = document.forms. f.txt.value;
var obj = document.getEle mentById("lyr") ;
obj.document.op en();
obj.document.wr ite(str);
obj.document.cl ose();
}
---

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.layer s' 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.sci entist.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
With all due respect to Mr. McGrath's copyright (comments ommited for
brevity):

document.write( "<div id='lyr'
style="position :absolute;top:9 0px;left:10px;w idth:200px;heig ht:10px;z-index:
10;background-color:orange'> </div>");

function write_entry()
{
var str = document.forms. f.txt.value;
var obj = document.getEle mentById("lyr") ;
obj.document.op en();
obj.document.wr ite(str);
obj.document.cl ose();
}
---

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
3610
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 file which comes with the samples: ---- C:\Python23\Lib\site-packages\wx\samples\wxProject\wxProject.py ---- It basically consists of a...
1
7457
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 the query analyzer and get the foloowing error;
7
4071
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 someone will be able to make suggestions anyway. I'm using .NET, the server is using Axis. The web method I'm calling returns a simple complex...
2
5834
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 properties" The reason this confuses me is that getAttribute("rel") returns a string, so it should have all the properties and methods of a string,...
4
2619
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 iframe's src property. Being that IE does not follow standard and considers an id, name, etc as a qualifying identifier for the document.getElementById...
1
2655
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 main list) to open up the full listing (sublist) below that headline. My JavaScript is almost but not quite working as needed. It also serves up an...
17
2763
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 Bad. So maybe we've got over that. I suppose properties could have Bad consequences if a user doesn't know they exist and think that a certain...
6
2047
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 <script language="javascript"> var form = document.forms; function showNameObject() {
0
1830
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... Did you do something to add wx to your PYTHONPATH? Maybe do "import sys; print sys.path" or "import os; print os.environ" to verify this. ...
0
7780
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7694
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8021
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7787
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3732
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1303
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.