Posted By

jatkins on 04/25/11


Tagged

quote blog rotator


Versions (?)

Blog Quote Rotator


 / Published in: JavaScript
 

Released into the public domain. To use, first call initializeQuoteRotator(rotatorID, citeTagID, containerParentID); where rotatorID is the ID of the element that has the text of the quote directly inside it; citeTagID is the ID of the element that has the citation directly inside it; and containerParentID is the ID of the element with both rotator and citeTag inside it (this allows for hiding the entire block with CSS, then unhiding once the quote has loaded. Then call addQuote(quoteText, quoteAuthor); (relatively self-explantory -- both are strings) for as many quotes as you wish to include in the rotation. Finally, call rotateQuotes(); to randomly select and display one of the quotes. When you are adding quotes, remember that your strings must be escape if they include certain characters -- e.g. to add 'It's cool' (by Bob) you would type addQuote('It\'s cool', 'Bob'); -- note the backslash before the apostrophe -- this is what 'escaping' is.

  1. /* Created April 24, 2011; updated April 26, 2011 */
  2. /* Released into the public domain by Josh Atkins */
  3.  
  4. var quoteContainer, quoteCiteTag, quoteContainerParent, quotes = [], quoteRotatorInitialized = 0, citePrefix;
  5.  
  6. function initializeQuoteRotator(quoteContainerID, quoteCiteTagID, quoteContainerParentID, strCitePrefix) {
  7. quoteContainer = quoteContainerID;
  8. quoteCiteTag = quoteCiteTagID;
  9. quoteContainerParent = quoteContainerParentID;
  10. quoteRotatorInitialized = 1;
  11. citePrefix = typeof strCitePrefix != 'undefined' && strCitePrefix ? strCitePrefix : '';
  12. }
  13.  
  14. function rotateQuotes() {
  15. if(quoteRotatorInitialized==1) {
  16. var quoteToShow = parseInt(Math.random() * quotes.length);
  17. if(quoteToShow>quotes.length) quoteToShow--;
  18. document.getElementById(quoteContainer).innerHTML = quotes[quoteToShow][0];
  19. document.getElementById(quoteCiteTag).innerHTML = citePrefix + quotes[quoteToShow][1];
  20. document.getElementById(quoteContainerParent).style.display = 'block';
  21. document.getElementById(quoteContainerParent).style.visibility = 'visible';
  22. }
  23. }
  24.  
  25. function addQuote(quoteText, quoteAuthor) {
  26. quotes.push([quoteText.replace(/<[^>]+?>/g, ''), quoteAuthor.replace(/<[^>]+?>/g, '')]);
  27. }

Report this snippet  

You need to login to post a comment.