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

document.body.innderHTML

Can someone please tell me why this works:

document.body.innerHTML =
"<table><tr><th>one</th></tr><tr><td>two</td></tr></table>";

But this does not work:
document.body.innerHTML = "<table><tr>";
document.body.innerHTML = document.body.innerHTML +
"<th>one</th></tr><tr><td>two</td></tr></table>";

Thanks

Sep 20 '05 #1
14 4036
wrote on 20 sep 2005 in comp.lang.javascript:
Can someone please tell me why this works:

document.body.innerHTML =
"<table><tr><th>one</th></tr><tr><td>two</td></tr></table>";

But this does not work:
document.body.innerHTML = "<table><tr>";
document.body.innerHTML = document.body.innerHTML +
"<th>one</th></tr><tr><td>two</td></tr></table>";


Perhaps because you have the script inside the body,
and that script is lost in execution of
the first line of your second example.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 20 '05 #2

<ch****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Can someone please tell me why this works:

document.body.innerHTML =
"<table><tr><th>one</th></tr><tr><td>two</td></tr></table>";

But this does not work:
document.body.innerHTML = "<table><tr>";
document.body.innerHTML = document.body.innerHTML +
"<th>one</th></tr><tr><td>two</td></tr></table>";


you would need "document,body..." is javascript and you are just creating a
table and adding text to it the document,body stuff will not be processed.
Sep 20 '05 #3
Evertjan. wrote:
Perhaps because you have the script inside the body,
and that script is lost in execution of
the first line of your second example.


Thanks for trying to help, but no, I'm not quite that stupid.

I'm not sure if I can cut and paste html through google groups. I'm
not sure if this is going to show up or not.

<html>
<head>
<script language="JavaScript">

function duh() {
// this will work:
document.body.innerHTML =
"<table><tr><th>one</th></tr><tr><td>two</td></tr></table>";

// this will not work (uncomment it to see for yourself
/*
document.body.innerHTML = "<table><tr>";
document.body.innerHTML = document.body.innerHTML +
"<th>one</th></tr><tr><td>two</td></tr></table>";
*/
}
</script>
</head>
<body onload="duh()">
</body>
</html>

Sep 20 '05 #4

Zoe Brown wrote:
you would need "document,body..." is javascript and you are just creating a
table and adding text to it the document,body stuff will not be processed.


I'm sorry, but I really can't parse that sentence. Can you explain a
little more please?

Sep 20 '05 #5
To partially answer my own question. The answer seems to have
something to do with error correcting code in the browser. If you set
body.innerHTML equal to something that has a table start tag but no
table end tag, the browser (I'm using IE, Firefox, and Opera) seems to
close the table for you - because it assumes you made a mistake. Then
later, when you add additional table data cells, the browser strips it
all out because, again, it assumes you made a mistake. You can't add
<td></td> because there is no open table.

Sep 20 '05 #6
wrote on 20 sep 2005 in comp.lang.javascript:
Evertjan. wrote:
Perhaps because you have the script inside the body,
and that script is lost in execution of
the first line of your second example.


Thanks for trying to help, but no, I'm not quite that stupid.

I'm not sure if I can cut and paste html through google groups. I'm
not sure if this is going to show up or not.

<html>
<head>
<script language="JavaScript">

function duh() {
// this will work:
document.body.innerHTML =
"<table><tr><th>one</th></tr><tr><td>two</td></tr></table>";

// this will not work (uncomment it to see for yourself
/*
document.body.innerHTML = "<table><tr>";
document.body.innerHTML = document.body.innerHTML +
"<th>one</th></tr><tr><td>two</td></tr></table>";
*/
}
</script>
</head>
<body onload="duh()">
</body>
</html>


The first one will give this correct result:

<BODY onload=duh()>
<TABLE>
<TBODY>
<TR><TH>one</TH></TR>
<TR><TD>two</TD></TR>
</TBODY>
</TABLE>
</BODY>

=====================

The second one, the corrupted one, this:

<BODY onload=duh()>
<TABLE>
<TBODY>
<TR></TR></TBODY></TABLE>
<TH>one</TH></TR>
<TR><TD>two</TD></TR>
</TABLE>
</BODY>

======================

You see that the browser [IE in my case] adds code on its own,
trying to make sense of the partial table set by
document.body.innerHTML = "<table><tr>";

and the added </TR></TBODY></TABLE> will corrupt your code
after the document.body.innerHTML = document.body.innerHTML + ..

======================
Try this as a improvement:

t = "<table><tr>";
t += "<th>one</th></tr><tr><td>two</td></tr></table>";

document.body.innerHTML = t;

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 20 '05 #7

Evertjan. wrote:
wrote on 20 sep 2005 in comp.lang.javascript:
Try this as a improvement:

t = "<table><tr>";
t += "<th>one</th></tr><tr><td>two</td></tr></table>";

document.body.innerHTML = t;


Yep. That's what I ended up doing. I just wanted to understand the
problem first. Thanks again for the help.

Sep 20 '05 #8
ch****************@gmail.com wrote:
To partially answer my own question. The answer seems to have
something to do with error correcting code in the browser. If you set
body.innerHTML equal to something that has a table start tag but no
table end tag, the browser (I'm using IE, Firefox, and Opera) seems to
close the table for you - because it assumes you made a mistake. Then
later, when you add additional table data cells, the browser strips it
all out because, again, it assumes you made a mistake. You can't add
<td></td> because there is no open table.


As suggested in Microsoft's documentation on innerHTML, don't use it for
tables.

<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/innerhtml.asp>

You can use it to write an entire table in one go (as proposed by
Evertjan), but you can't use it to write bits of tables - I think you
can get away with it to some extent in Firefox but that is utterly
unreliable.

The reason behind the behaviour you observed is open to conjecture - you
may well be correct, but since there is no public standard on how
innerHTML is supposed to work, each browser has implemented its own
version resulting in some considerable variation. Some browsers might
ignore incomplete tags and not try to create elements from them, others
may attempt to correct the HTML and insert missing tags according to
some unpublished algorithm.

You are best to use innerHTML for very simple things like writing text
strings or simple markup. Use DOM methods for complex stuff.

--
Rob
Sep 20 '05 #9

<ch****************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Zoe Brown wrote:
you would need "document,body..." is javascript and you are just creating
a
table and adding text to it the document,body stuff will not be
processed.


I'm sorry, but I really can't parse that sentence. Can you explain a
little more please?


sorry, had a few drinks.

you are writing javascript out in the body tag and this would be parsed. I
think others have explained this better.

but its like writeing

document.body.innerHTML = alert("hello");

it wont work cos the alert is javascript and just plonking it in the body
tag does not cause it to be executed.

Zoe
Sep 21 '05 #10
Zoe Brown wrote:
you are writing javascript out in the body tag


No, I'm not. I don't know where you got that idea.

Sep 21 '05 #11
RobG wrote:

You are best to use innerHTML for very simple things like writing text
strings or simple markup. Use DOM methods for complex stuff.


What I'm actually doing is updating a calendar that pops up in an
iframe. There are "next month" and "previous month" buttons and when
the user clicks one of those the calendar should redraw itself with the
new month.

Now, I *could* go through each cell and change the contents with DOM
calls. But since this is just a very simple 7x4 table, I thought that
everything would look a lot cleaner if I just completely rewrote the
body.

But you're saying that's a bad idea?

Sep 21 '05 #12
ch****************@gmail.com wrote:
RobG wrote:
You are best to use innerHTML for very simple things like writing text
strings or simple markup. Use DOM methods for complex stuff.

What I'm actually doing is updating a calendar that pops up in an
iframe. There are "next month" and "previous month" buttons and when
the user clicks one of those the calendar should redraw itself with the
new month.

Now, I *could* go through each cell and change the contents with DOM
calls. But since this is just a very simple 7x4 table, I thought that
everything would look a lot cleaner if I just completely rewrote the
body.

But you're saying that's a bad idea?


My comment was in regard to using innerHTML to modify only part of a
table. Using innerHTML for rebuilding the entire calendar isn't so much
good or bad, but that there are better ways.

Browsers are optimised to parse and render HTML very quickly, so in
terms of speed, using innerHTML tends to be very fast. However, you
must first construct the HTML and the resources for that must also be
factored in.

I think it would be more efficient to build the table once using HTML,
then modify the text in the calendar cells for each change of month.
That could be done using innerHTML but probably more efficiently by
building the table once and subsequently modifying just the text nodes.

Consider creating an array of references to the text nodes for the date
cells, then just modify those. The table is built once only, as is the
array of references. You may also want to have different styles for
cells with dates, cells without and weekends.

You need a bigger table than 7x4 - some months may span up to 6 weeks
(e.g. a 31 day month starting on a Saturday). I built a calendar along
similar lines recently, I'll dig it out and post it if you like. It ran
virtually instantaneously even on very old machines.

The algorithm is simple - loop through the cell reference array blanking
cells until you get to the the day number of the first day of the month
(adjusting for Sunday or Monday as the first day of the week is simple).
Put dates starting from 1 in the following cells for each day of the
month until the last, then blank the remaining cells to the end. Often
you'll have a full row empty at the bottom - maybe you'll want to hide
the row in that case.

I can't imagine that the algorithm for creating the HTML would be
simpler, especially if the table also has labels for days, weeks, year,
month name, next/previous month/year buttons, changing styles for
weekends, etc.
--
Rob
Sep 21 '05 #13
RobG wrote:

I can't imagine that the algorithm for creating the HTML would be
simpler


It probably isn't. It's just very straightforward and so it was the
first thing that I came up with. At any rate, I'm only doing this for
my own personal edification. I finished the calendar portion of the
project I was getting paid to do. Now I'm just spending extra time on
it as sort of a professional development thing.

I like your idea of an array of pointers to the date cells. I'm going
to redo it that way. Thanks for the tip!

Sep 21 '05 #14
JRS: In article <4331729e$0$11733$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Thu, 22 Sep 2005 00:47:09, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.au> posted :

You need a bigger table than 7x4 - some months may span up to 6 weeks
(e.g. a 31 day month starting on a Saturday).


That does depend on the definition of a week, and of which weeks belong
to which months. If week-of-month is defined analogously to ISO 8601
week-of-year, there will be either 4 or 5 weeks for each month number.

As alternatives to using a Table for the month, one should consider a
<pre> section and possibly a <textarea>; I have used a block of Buttons.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 22 '05 #15

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

Similar topics

4
by: Dante | last post by:
Hello. I have a Javascript that gets data from an XML document and displays it through javascript. The problem is that when I do dcfile.getElementsByTagName("subhead").firstChild.nodeName all I...
4
by: Bernard | last post by:
Hi, I am suddenly getting Safari script errors with the following user agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.2 (KHTML, like Gecko) Safari/125.8 In a...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
2
by: Earl Teigrob | last post by:
I am trying to build a custom control to wrap my smart navigation implimention (not microsofts 'cause it has problems) The follow code works fine when the onclick and onload events are defined in...
6
by: Rob | last post by:
Hello, I'm sure this has come up before. I have need for a collection of all elements/objects in an HTML document that have any kind of an attribute (HTML or CSS) that is making use of a URL to...
5
by: jhurrell | last post by:
I have been having some trouble getting my XSL style sheet to parse correctly. I have some XML outputted from an SQL-Server, that I then need to turn into multiple HTML files. This I have done...
11
Dormilich
by: Dormilich | last post by:
Lately I have seen so much awful HTML, that I like to show what a HTML document should look like, regarding the requirements from the W3C. the absolute minimum is defined as: or expressed in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.