homepage Welcome to WebmasterWorld Guest from 54.211.73.232
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
document.write refreshes page (workaround needed)
document.write refresh page workaround
cookiemonster



 
Msg#: 4046712 posted 2:08 am on Dec 21, 2009 (gmt 0)

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);
document.write(text[increment]);

//--></SCRIPT>

Thanks in advance!

 

techtheatre

5+ Year Member



 
Msg#: 4046712 posted 4:25 am on Dec 21, 2009 (gmt 0)

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:

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

then use this js line to plug in the data:
document.getElementById('DIVNAMEHERE').innerHTML=text[increment];

techtheatre

5+ Year Member



 
Msg#: 4046712 posted 4:27 am on Dec 21, 2009 (gmt 0)

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.

cookiemonster



 
Msg#: 4046712 posted 4:12 pm on Dec 21, 2009 (gmt 0)

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!

chasehx

5+ Year Member



 
Msg#: 4046712 posted 5:48 pm on Dec 23, 2009 (gmt 0)

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...
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved