Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

my attempt at making a random text line script

Status
Not open for further replies.

wiser3

Programmer
Jun 3, 2003
358
CA
First of all here the html calling the external javascript:

< script type=&quot;text/javascript&quot; src=&quot;js/textlines.js&quot;></script>

And here is my javascript from the file above:

// set up lines of html in an array

textcontent[0] = '<p class=&quot;swapcelltextbig&quot;>This is texcontent 0.</p><p class=&quot;swapcelltext&quot;>another paragraph?</p><p class=&quot;swapcelltext&quot;>final paragraph?</p>'
textcontent[1] = '<p class=&quot;swapcelltextbig&quot;>This is texcontent 1.</p><p class=&quot;swapcelltext&quot;>another paragraph?</p><p class=&quot;swapcelltext&quot;>final paragraph?</p>'
textcontent[2] = '<p class=&quot;swapcelltextbig&quot;>This is texcontent 2.</p><p class=&quot;swapcelltext&quot;>another paragraph?</p><p class=&quot;swapcelltext&quot;>final paragraph?</p>'
textcontent[3] = '<p class=&quot;swapcelltextbig&quot;>This is texcontent 3.</p><p class=&quot;swapcelltext&quot;>another paragraph?</p><p class=&quot;swapcelltext&quot;>final paragraph?</p>'

// measure array size and randomly pick one
var textshow=textcontent.length
var currenttext=Math.floor(Math.random()*textshow)

//write the picked html into the page
document.write('currenttext')


Please help me get this working. I'm new to JavaScript, and thought this would be a simple way to start using it.
 
two errors:
1. you need to declare textcontent as an array first
2. your document.write() statement wasn't going to do what you want

with corrections:
// set up lines of html in an array
textcontent = [];
textcontent[0] = '<p class=&quot;swapcelltextbig&quot;>This is texcontent 0.</p><p class=&quot;swapcelltext&quot;>another paragraph?</p><p class=&quot;swapcelltext&quot;>final paragraph?</p>'
textcontent[1] = '<p class=&quot;swapcelltextbig&quot;>This is texcontent 1.</p><p class=&quot;swapcelltext&quot;>another paragraph?</p><p class=&quot;swapcelltext&quot;>final paragraph?</p>'
textcontent[2] = '<p class=&quot;swapcelltextbig&quot;>This is texcontent 2.</p><p class=&quot;swapcelltext&quot;>another paragraph?</p><p class=&quot;swapcelltext&quot;>final paragraph?</p>'
textcontent[3] = '<p class=&quot;swapcelltextbig&quot;>This is texcontent 3.</p><p class=&quot;swapcelltext&quot;>another paragraph?</p><p class=&quot;swapcelltext&quot;>final paragraph?</p>'

// measure array size and randomly pick one
var textshow=textcontent.length
var currenttext=Math.floor(Math.random()*textshow)

//write the picked html into the page
document.write(textcontent[currenttext])

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
WOW, thanks - your fixes work perfectly.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top