Welcome to WebmasterWorld Guest from 107.20.75.63

Forum Moderators: open

Message Too Old, No Replies

Dynamically Updating Body Text with Javascript

How would you add to the body text after a page has loaded?

     
1:29 pm on Jun 13, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2002
posts:82
votes: 0


I've got a program written in javascript that takes a while to load and I want to display the output sequentially, rather than making the user wait until the program has completed. Is there a good way, using javascript, to update the body text after the page has loaded?
4:30 pm on June 13, 2003 (gmt 0)

New User

10+ Year Member

joined:Apr 5, 2003
posts:17
votes: 0


write() method
4:31 pm on June 13, 2003 (gmt 0)

New User

10+ Year Member

joined:Apr 5, 2003
posts:17
votes: 0


document.write("Dog");
10:20 am on June 14, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 27, 2003
posts:664
votes: 0


Take care... If you use document.write() after the page has loaded it will completely rewrite (i.e. obliterate) the page, not append to it...

To "...update the body text after it has loaded...", do as follows:

Say in your html you have <div id="blah">some text</div>

Then in your javascript which gets executed using the onload method:
document.getElementById("blah").innerHTML = "Different Text";

Also, you can add child nodes (e.g. divs or whatever tag you want) with createElement() and appendChild()
e.g. say you have <div id="table_holder"> </div>
Then you can do:
var table_div = document.getElementById("table_holder");
var table_to_insert = document.createElement("table");
table_div.appendChild(table_to_insert);

Similarly, you can add tr's as children of the table_to_insert and td's as their children... or whatever tags you want to add.

Note that all this may not help with your initial problem (getting the page to load faster). All the javascript needs to get downloaded... Describe what you are trying to do in more detail, and someone may be able to offer more help.

Hope that makes sense

Shawn

11:54 am on June 14, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2003
posts:44
votes: 0


You could look into innerHTML for this.