homepage Welcome to WebmasterWorld Guest from 54.225.1.70
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

    
Script to change text section
Ben502




msg:4332717
 8:34 pm on Jun 29, 2011 (gmt 0)

I am looking for a quick and dirty script to change out a bit of text each time a page loads...for example, say i had 10 quotes. Each time the page loads i would like to load a different quote. i think i would prefer to do this with text instead of making them images. Thoughts?

THanks y'all

 

penders




msg:4332751
 9:33 pm on Jun 29, 2011 (gmt 0)

Ok, something quick and dirty.... ;)

Save this JavaScript in a file called "random_quotes.js":

/** 
* Random Text / Quotes
* Written to element with ID="text-goes-here"
*/
var quotes = [
"To be or not to be, that is the question!",
"Space - the final frontier!",
"Jack and Jill went up the hill..."
];
var obj = document.getElementById('text-goes-here');
if (obj) {
var index = Math.floor(Math.random() * quotes.length);
obj.innerHTML = quotes[index];
}


Link to this script at the bottom of your HTML page, above the closing </body> tag:
<script type="text/javascript" src="random_quotes.js"></script>


Then, in your page, wherever you want the random text/quote to appear put this:
<div id="text-goes-here"></div>


Refresh your page!

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