Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

document.write refreshes page (workaround needed)

document.write refresh page workaround



2:08 am on Dec 21, 2009 (gmt 0)

5+ Year Member

I have a JavaScript that uses document.write to well ... write something to the page.
My problem is that, as you probably know, document.write reloads the whole page.
I'm pretty sure you can use something like document.getElementById to accomplish the same thing, more or less, but I'm not sure of the proper way to do that.

Here is my code:

<SCRIPT LANGUAGE="Javascript"><!--

function text() {

text = new text();
number = 0;

// textArray

text[number++] = "random text 1"
text[number++] = "random text 2"
text[number++] = "random text 3"
text[number++] = "random text 4"
text[number++] = "random text 5"

// keep adding items here...

increment = Math.floor(Math.random() * number);


Thanks in advance!


4:25 am on Dec 21, 2009 (gmt 0)

5+ Year Member

I am far from a JavaScript guru...but try this:

add a <div> in your page source where you want to put this text. maybe like this:

<p>stuff on page</p>
<div id="DIVNAMEHERE"></div>
<p>more stuff here too</p>

then use this js line to plug in the data:


4:27 am on Dec 21, 2009 (gmt 0)

5+ Year Member

you also seem to be closing your function before you do anything.

function text() {

i think that the closing bracket should be after all your processing, etc.


4:12 pm on Dec 21, 2009 (gmt 0)

5+ Year Member

Thanks, techtheatre ... I got it figured out now - I have a div with id "result", and I ended my JavaScript with document.getElementById('result').innerHTML = (text[increment]);

Of course, now I have another problem lol... I only want the JavaScript to execute onclick of a button, but right now it loads as the page loads. I'll figure that one out though.

Thanks again for the help!


5:48 pm on Dec 23, 2009 (gmt 0)

5+ Year Member

That issue is because the brackets are in the wrong place. In JavaScript, if no functions are defined and directives are left in the root level of the script tag, they will be executed onload.

function yourFunction(included_vars){
...actions here...


Featured Threads

Hot Threads This Week

Hot Threads This Month