472,347 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

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 4118

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the...
32
by: jp29 | last post by:
My take on problems composing, serving and rendering XHTML documents/web pages: 1. Typical conscientious web authors are producing XHTML...
1
by: arun32581 | last post by:
I am developing a page for Mozilla/IE which reads xml data and when the link on the page is clicked it displays the data as a table. The display is...
3
by: arun32581 | last post by:
I am developing a page for Mozilla/IE which reads xml data and when the link on the page is clicked it displays the data as a table. The display is...
1
by: alex.sherwin | last post by:
Working on a google homepage module. One of the things I do is retrieve results from a search into a string. I want to be able to access all the...
1
by: JackieWilson | last post by:
Hello! I', trying to create DOM based XML document. First I create XmlDocument object and then read other XML DOM Document and modifie the first...
10
by: James Black | last post by:
It appears that this is actually a difference between whether to use DOM2 or DOM1. I am trying to write my programs using XHTML for the webpage,...
4
by: Narven | last post by:
Hi, I've been creating a script that dynamic loads js files. but after creating that script, (and i use document.createElement('script');) in...
23
by: Stanimir Stamenkov | last post by:
<form name="myForm" action="..."> <p><input type="text" name="myElem"></p> </form> As far as I was able to get the following is the standard way...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.