469,625 Members | 1,813 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Problems with document.createElement() in XHTML

Hello, I try the following in Firefox and other modern browsers:

window.addEventListener('load', function() {
document.title = CSS.getClass('fontSize');
var div = document.createElement('div');
document.getElementsByTagName('body')[0].appendChild(div);
alert(div);
alert(div.style)
}, true);

It works fine in normal HTML mode (Content-type: text/html), but in
XHTML mode it alerts "[object Element]" instead of "[object
HTMLDivElement]" and the second alert shows "undefined" instead of
"[object CSSStyleDeclaration]". So I can't reach the style declaration
which is important for me. Strict mode makes trouble again and again,
the biggest bug: document.write does not work:
http://www.intertwingly.net/blog/200...hats-Not-Write

Can somebody help me with my problem?

Andi

Nov 25 '06 #1
10 3887

webEater wrote:
Hello, I try the following in Firefox and other modern browsers:

window.addEventListener('load', function() {
document.title = CSS.getClass('fontSize');
var div = document.createElement('div');
document.getElementsByTagName('body')[0].appendChild(div);
alert(div);
alert(div.style)
}, true);
Your indentation is confusing. Please clean it up next time.

>
It works fine in normal HTML mode (Content-type: text/html), but in
XHTML mode it alerts "[object Element]" instead of "[object
HTMLDivElement]" and the second alert shows "undefined" instead of
"[object CSSStyleDeclaration]". So I can't reach the style declaration
div doesn't have any style attribute; why should the second alert show
anything? It may have styles applied from a stylesheet, but that's not
the same as a style attribute. Also in strict XHTML mode you should
use node.getAttribute('style') rather than node.style. Remember you're
dealing with an XML document, not an HTML document. The XML DOM is
eversoslightly different than the HTML DOM and it might be worth
reviewing the differences.
the biggest bug: document.write does not work:
Not a bug. Don't use document.write in strict mode.

If the additional challenges of strict mode are too difficult, may I
ask why you're using strict mode? Why not just use normal HTML mode,
if that's working for you?

-David

Nov 25 '06 #2


On 25 Nov., 21:36, "David Golightly" <davig...@gmail.comwrote:
webEater wrote:
Hello, I try the following in Firefox and other modern browsers:
window.addEventListener('load', function() {
document.title = CSS.getClass('fontSize');
var div = document.createElement('div');
document.getElementsByTagName('body')[0].appendChild(div);
alert(div);
alert(div.style)
}, true);Your indentation is confusing. Please clean it up next time.


It works fine in normal HTML mode (Content-type: text/html), but in
XHTML mode it alerts "[object Element]" instead of "[object
HTMLDivElement]" and the second alert shows "undefined" instead of
"[object CSSStyleDeclaration]". So I can't reach the style declarationdiv doesn't have any style attribute; why should the second alert show
anything? It may have styles applied from a stylesheet, but that's not
the same as a style attribute. Also in strict XHTML mode you should
use node.getAttribute('style') rather than node.style. Remember you're
dealing with an XML document, not an HTML document. The XML DOM is
eversoslightly different than the HTML DOM and it might be worth
reviewing the differences.
the biggest bug: document.write does not work:Not a bug. Don't use document.write in strict mode.

If the additional challenges of strict mode are too difficult, may I
ask why you're using strict mode? Why not just use normal HTML mode,
if that's working for you?

-David
Hello David,

I am working on a framework that must work in every mode, html and xml.

the biggest bug: document.write does not work:Not a bug. Don't use document.write in strict mode.
This is very well-founded *laugh. I have written a workaround and use
it still ;) And I call it a bug.

But to my problem, I have tested all possibilities to add a style
declaration to my divs:

window.addEventListener('load', function() {
document.title = CSS.getClass('fontSize');
var div = document.createElement('div');
document.getElementsByTagName('body')[0].appendChild(div);
alert(div);
// brute force
//div.style.backgroundColor = 'red';
div.getAttribute('style').backgroundColor = 'red';
div.getAttribute('style').setAttribute('background Color', 'red');
div.getAttribute('style').setAttribute('background-color', 'red');
}, true);

None of them is working. I think there MUST be a way to add a style
declaration. It's the first time that I have this problem. The FireBug
error: div.getAttribute("style") has no properties

Nov 25 '06 #3

webEater wrote:
window.addEventListener('load', function() {
document.title = CSS.getClass('fontSize');
var div = document.createElement('div');
document.getElementsByTagName('body')[0].appendChild(div);
alert(div);
// brute force
//div.style.backgroundColor = 'red';
div.getAttribute('style').backgroundColor = 'red';
div.getAttribute('style').setAttribute('background Color', 'red');
div.getAttribute('style').setAttribute('background-color', 'red');
}, true);

None of them is working. I think there MUST be a way to add a style
declaration. It's the first time that I have this problem. The FireBug
error: div.getAttribute("style") has no properties
Number 1: Again, please fix your indentation! I pointed this out in
my last post and you repost your code without fixing it. This is not
ok.

Wow, you need to pick up a guide to the XML DOM, and quick. This isn't
how you add a style attribute - getAttribute returns a value, not a
reference, so trying to set its properties in this way is not going to
work. You should also learn some basic OOP language like C++ or Java
to learn that getX and setX are basic property interfaces that the DOM
is using here that generally work in values, not references. To wit:
you need to call

div.setAttribute('style', 'background-color: red; border: 1px solid
green;');

Nov 25 '06 #4
"David Golightly" <da******@gmail.comwrites:
Also in strict XHTML mode you should
use node.getAttribute('style') rather than node.style.
Could you explain why? I'm new here, trying to pick up good style :=)
Ari.

--
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
Nov 25 '06 #5

Ari Krupnik wrote:
"David Golightly" <da******@gmail.comwrites:
Also in strict XHTML mode you should
use node.getAttribute('style') rather than node.style.

Could you explain why? I'm new here, trying to pick up good style :=)
It's because XML nodes are different from HTML nodes. Specifically,
they don't have the JavaScript properties that you're used to in HTML
nodes. Most of the time however, when you're scripting in a browser,
you don't need to worry about treating your XHTML as a strict XML
document - even in strict mode, when you serve your document as
"Content-Type: text/html" you're actually going to get an HTML document
even with an XHTML doctype. However, when you override the
content-type (say, with a server directive or with a meta http-equiv
tag) and set it to text/xml, you're dealing with a different beast
altogether. Your node.id and node.style and most other built-in
javascript element properties disappear, document.getElementById
doesn't work like you expect it to (see:
http://blog.bitflux.ch/wiki/GetElementById_Pitfalls), and several other
habits have to be changed. "style" becomes just another attribute of
your XML document, and you need to treat it like one.

But most of the time, you're using XHTML served as "Content-Type:
text/html" and don't need to worry about these issues.

-David

Nov 25 '06 #6


On 25 Nov., 22:44, "David Golightly" <davig...@gmail.comwrote:
Ari Krupnik wrote:
"David Golightly" <davig...@gmail.comwrites:
Also in strict XHTML mode you should
use node.getAttribute('style') rather than node.style.
Could you explain why? I'm new here, trying to pick up good style :=)It's because XML nodes are different from HTML nodes. Specifically,
they don't have the JavaScript properties that you're used to in HTML
nodes. Most of the time however, when you're scripting in a browser,
you don't need to worry about treating your XHTML as a strict XML
document - even in strict mode, when you serve your document as
"Content-Type: text/html" you're actually going to get an HTML document
even with an XHTML doctype. However, when you override the
content-type (say, with a server directive or with a meta http-equiv
tag) and set it to text/xml, you're dealing with a different beast
altogether. Your node.id and node.style and most other built-in
javascript element properties disappear, document.getElementById
doesn't work like you expect it to (see:http://blog.bitflux.ch/wiki/GetElementById_Pitfalls), and several other
habits have to be changed. "style" becomes just another attribute of
your XML document, and you need to treat it like one.

But most of the time, you're using XHTML served as "Content-Type:
text/html" and don't need to worry about these issues.

-David
Thank you David for your help. So I think it's not worth to deal with
XML really. I think it's a little bit confusing to dispense with the id
attribute. So text/html is the choice.

window.addEventListener('load',
function() {
var b = document.getElementsByTagName('body')[0];
var d = document.createElement('div');
b.appendChild(d);
d.innerHTML = '<div id="yeah">Hello</div>';
alert(d.firstChild); // returns null, innerHTML doesn't work too
$('yeah').style.color = 'green';
},
true);

My experience is now that traditional HTML properties don't work with
dynamically created elements. Elements already existing in your
document still have all properties.

Nov 25 '06 #7


On 25 Nov., 23:52, "webEater" <andreaskal...@gmx.dewrote:
On 25 Nov., 22:44, "David Golightly" <davig...@gmail.comwrote:
Ari Krupnik wrote:
"David Golightly" <davig...@gmail.comwrites:
Also in strict XHTML mode you should
use node.getAttribute('style') rather than node.style.
Could you explain why? I'm new here, trying to pick up good style :=)It's because XML nodes are different from HTML nodes. Specifically,
they don't have the JavaScript properties that you're used to in HTML
nodes. Most of the time however, when you're scripting in a browser,
you don't need to worry about treating your XHTML as a strict XML
document - even in strict mode, when you serve your document as
"Content-Type: text/html" you're actually going to get an HTML document
even with an XHTML doctype. However, when you override the
content-type (say, with a server directive or with a meta http-equiv
tag) and set it to text/xml, you're dealing with a different beast
altogether. Your node.id and node.style and most other built-in
javascript element properties disappear, document.getElementById
doesn't work like you expect it to (see:http://blog.bitflux.ch/wiki/GetElementById_Pitfalls), and several other
habits have to be changed. "style" becomes just another attribute of
your XML document, and you need to treat it like one.
But most of the time, you're using XHTML served as "Content-Type:
text/html" and don't need to worry about these issues.
-DavidThank you David for your help. So I think it's not worth to deal with
XML really. I think it's a little bit confusing to dispense with the id
attribute. So text/html is the choice.

window.addEventListener('load',
function() {
var b = document.getElementsByTagName('body')[0];
var d = document.createElement('div');
b.appendChild(d);
d.innerHTML = '<div id="yeah">Hello</div>';
alert(d.firstChild); // returns null, innerHTML doesn't work too
$('yeah').style.color = 'green';},true);

My experience is now that traditional HTML properties don't work with
dynamically created elements. Elements already existing in your
document still have all properties.
My cross-browser solution, includes IE 5.5+:

onload = function() {
var div = document.createElement('div');
document.getElementsByTagName('body')[0].appendChild(div);
var t = document.createTextNode('text');
div.appendChild(t);
div.setAttribute('style', 'color:green');
if (div.style.color == '')
try { div.style.color = 'green'; } catch(e) {}
alert(div.style.color);
};

Nov 25 '06 #8
David Golightly wrote:
webEater wrote:
>Hello, I try the following in Firefox and other modern browsers:

window.addEventListener('load', function() {
document.title = CSS.getClass('fontSize');
var div = document.createElement('div');
document.getElementsByTagName('body')[0].appendChild(div);
alert(div);
alert(div.style)
}, true);

Your indentation is confusing. Please clean it up next time.
>It works fine in normal HTML mode (Content-type: text/html), but in
XHTML mode it alerts "[object Element]" instead of "[object
HTMLDivElement]" and the second alert shows "undefined" instead of
"[object CSSStyleDeclaration]". So I can't reach the style
declaration

div doesn't have any style attribute;
DIV elements most certainly do have style attributes (both in HTML and
XHTML).

<snip>
... . Also in strict XHTML mode you
should use node.getAttribute('style') rather
than node.style.
There is no reason for not using the - style - property of XHTML
elements, but if a method is to be used instead it should be the -
setAttributeNS - method that is used (XHTML attributes being namespace
qualified).
Remember you're dealing with an XML document, not an HTML
document.
It is an XHTML document, which may be XML but is not only that. XHTML
1.0 DOMs should be expected to implement at least most of the W3C HTML
DOM specification in addition to the Core DOM.
The XML DOM is eversoslightly different than the HTML DOM
This is "eversoslightly" in the sense of; so divergent that no
non-trivial HTML DOM script can be expected to work in an XHTML DOM and
the effort needed to create a script that will function in both is so
great that no (knowledgeable) script author would consider proposing the
use of (real) XHTML in a commercial context.
and it might be worth reviewing the differences.
Good advice, that if followed would enable you to avoid making false
statements about what should and should not exist in an XHTML DOM, and
what should and should not be done with one.
>the biggest bug: document.write does not work:

Not a bug. Don't use document.write in strict mode.

If the additional challenges of strict mode
Normally 'strict mode' is used to distinguish between browsers applying
correct rules in the interpretation of CSS and browsers applying
differing rules for reasons of back-compatibility. Here the distinction
is between scripting an HTML DOM and scripting an XHTML (though XHTML
DOMs can expect to have CSS rules applied strictly).
are too difficult, may I ask why you're using strict mode?
Why not just use normal HTML mode, if that's working for you?
Authoring and scripting HTML documents elusively is a sensible strategy
for dealing with the dual problems that no IE browser (including 7) is
capable of interpreting XHTML as XHTML (providing an XHTML DOM to be
scripted) and that the effort needed to attempt to create and maintain
scripts that will function with both types of DOM is disproportional, as
is creating and maintaining two separate scripts, one for each type of
DOM (and so neither can easily be commercially justified).

Richard.
Nov 26 '06 #9
webEater wrote:
Hello, I try the following in Firefox and other modern browsers:

window.addEventListener('load', function() {
document.title = CSS.getClass('fontSize');
var div = document.createElement('div');
In an XHTML document you should be suing the - createElementNS - method,
with a second argument supplying the namespace for XHTML.
document.getElementsByTagName('body')[0].appendChild(div);
alert(div);
alert(div.style)
}, true);

It works fine in normal HTML mode (Content-type: text/html),
but in XHTML mode it alerts "[object Element]" instead of
"[object HTMLDivElement]"
Seems reasonable as you did not tell the DOM which namespace (i.e.
XHTML) your 'div' element belongs to.
and the second alert shows "undefined" instead of
"[object CSSStyleDeclaration]".
<snip>

Well, as it is not an XHTML element you should not expect it to have
interfaces specific to particular types of element. DOM Core interfaces
are the most you can expect.

Richard.
Nov 26 '06 #10
David Golightly wrote:
Ari Krupnik wrote:
>"David Golightly" <da******@gmail.comwrites:
Also in strict XHTML mode you should
use node.getAttribute('style') rather than node.style.

Could you explain why? I'm new here, trying to pick up
good style :=)

It's because XML nodes are different from HTML nodes.
<snip>

While the object created here probably is just an XML node the object
that should have been created (with the - createElementNS - method)
should be expected to implement appropriate interfaces from the W3C HTML
DOM, including the - style - property. The W3C HTML DOM specification
explicitly applies to XHTML 1.0, and lists where XHTML DOMs will differ
from HTML DOMs.
But most of the time, you're using XHTML served as
"Content-Type: text/html" and don't need to worry about
these issues.
A document served as text/html is not XHTML in anything but appearance.
It will be interpreted as tag-soup HTML and all the XML-like features of
the mark-up will be error-corrected back to something that makes sense
in tag-soup HTML.

Richard.
Nov 26 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by daveland | last post: by
32 posts views Thread by jp29 | last post: by
1 post views Thread by arun32581 | last post: by
3 posts views Thread by arun32581 | last post: by
1 post views Thread by alex.sherwin | last post: by
1 post views Thread by JackieWilson | last post: by
23 posts views Thread by Stanimir Stamenkov | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.