Thursday, June 7, 2007

Javascript Example code for creating "Private" member variables using closures and JSON notation:

Title: Javascript Example code for creating Objects with Public and Private “member” variables, using closures and JSON notation:

 

Wrote this bit of cool code today to rotate through a series of third party quotes .  Figured I should make a post of this most excellent function structure.

 

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////

// SETTINGS - change these values to alter the behavior of how,what and when QuoteSwapper displays quotes

// --------

var g_FirstQuoteToDisplay = 4;          // Sets what quote number is first displayed when the page loads

var g_QuoteRotationEnabled = true;      // Setting this to false will prevent QuoteSwapper from rotating and will; only display the first quote specified

var g_SecondsToDisplayEachQuote = 3;    // How many seconds each quote is displayed before rotating to the next

var g_Quotes = [

  { // Quote #1                

    quoteText: "thanks to thinkingVOICE's Call Tracking Platform we can demonstrate added value to our participating merchants every day",

    quoteSource: "Kendall Fargo CEO",

    quoteLogo: "logo_stepupCommerce.gif",

    quoteLogoX: 101,

    quoteLogoY: 53,

    quoteLogoAlt: "StepUp Logo (an Intuit Company)",

    quoteURL: "http://www.stepup.com/",

    quoteTagLine: "an Intuit Company",

    specialFrameAction : null  

  },

  { // Quote #2

   quoteText: "ThinkingVoice enables Commercial Direct to qualify leads to our sales reps and close the deal quickly. By offering timeliness and convenience to our prospects, thinkingVoice has paid off big time",

   quoteSource: "Jonathan Mirabito",

   quoteLogo: "logo_commercialdirect.jpg",

   quoteLogoX: 160,

   quoteLogoY: 52,

   quoteLogoAlt: "Commercial Direct Loans Logo",

   quoteURL: "http://www.commercialdirectloans.com/index.jsp?pageId=home",

   quoteTagLine: "",  

   specialFrameAction : null  

  },

  { // Quote #3

   quoteText: "With a simple click, potential customers connect to a live sales representative, get immediate answers to their questions and can register for a WebEx Live! service with confidence",

   quoteSource: "Alfred Pong",

   quoteLogo: "logo_webex.gif",

   quoteLogoX: 160,

   quoteLogoY: 60,

   quoteLogoAlt: "WebEx Logo",

   quoteURL: "http://www.webex.com/",

   quoteTagLine: "",

   specialFrameAction : null     

  },

  { // Quote #4

   quoteText: "adding thinkingVOICE to my email signature just makes good business sense.  And it works so easy with PLAXO.",

   quoteSource: "Mark Goldstein",

   quoteLogo: "logo_loyaltylab.gif",

   quoteLogoX: 160,

   quoteLogoY: 42,

   quoteLogoAlt: "LoyaltyLab, a division of InStoreCard",

   quoteURL: "http://www.instorecard.com/",

   quoteTagLine: ""  

  },

  { // Quote #5  

   quoteText: '<span style="font-weight:bolder;color:brown;font-family:arial mt rounded; font-size:1em;line-height:.9em;text-align:center;vertical-align:baseline;">Breaking News<br/></span><span style="line-height:1em; font-size:10px; font-family:lucida sans unicode, tahoma;margin:0;">ThinkingVoice announces deals with <a href="http://www.clearchanneloutdoor.com/" target="top" class="announcement_link">ClearChannel Outdoor</a>, <a href="http://www.servicemagic.com/" target="_top" class="announcement_link">Service Magic</a></span>',

   quoteSource: '',

   quoteLogoX: 194,

   quoteLogoY: 290,

   quoteLogo: "images/CLearChannelBillboard.gif",

   quoteLogoAlt: "ThinkingVoice announces deals with ClearChannel outdoor and Service Magic",

   quoteURL: "http://sev.prnewswire.com/advertising/20070605/AQTU18605062007-1.html",

   quoteTagLine: "source: prnewswire",

   specialFrameAction : null  

  }

]

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

// QUOTESWAPPER - this Object cycles through and displays an array of quotes, images and hyperlinks.

// ============   DO NOT MODIFY CODE IN THIS SECTION  

var QuoteSwapper = function() {

  var self = this;

  var _swapperThread = null;

  var _bRotationEnabled = g_QuoteRotationEnabled;

  var _currentItem = g_FirstQuoteToDisplay;

 

  function public() {}

 

  public.displayQuote = function( itemToDisplay ) {

    itemToDisplay = (("" + itemToDisplay!="undefined") ? itemToDisplay : g_FirstQuoteToDisplay ) % g_Quotes.length;  // stay within limits of array

    document.getElementById('quoteText').innerHTML = (g_Quotes[ itemToDisplay ].quoteText.length > 0) ? g_Quotes[ itemToDisplay ].quoteText : "";

    document.getElementById('quoteSource').innerText = (g_Quotes[ itemToDisplay ].quoteSource.length > 0) ? " - " + g_Quotes[ itemToDisplay ].quoteSource : "";

    document.getElementById('quoteLogo').src = g_Quotes[ itemToDisplay ].quoteLogo;

    document.getElementById('quoteLogo').width = g_Quotes[ itemToDisplay ].quoteLogoX;

    document.getElementById('quoteLogo').height = g_Quotes[ itemToDisplay ].quoteLogoY;

    document.getElementById('quoteLogo').alt = g_Quotes[ itemToDisplay ].quoteLogoAlt;

    document.getElementById('quoteURL').href = g_Quotes[ itemToDisplay ].quoteURL;

    document.getElementById('quoteTagLine').innerHtml = g_Quotes[ itemToDisplay ].quoteTagLine;

    _currentItem = itemToDisplay;

  }

 

  public.start = function() {

    clearInterval( _swapperThread );

    if ( _bRotationEnabled ) {

      _swapperThread = setInterval("QuoteSwapper.next()", g_SecondsToDisplayEachQuote * 1000);   

    }

    this.displayQuote( g_FirstQuoteToDisplay );   

  }

 

  public.next = function() {

    this.displayQuote( _currentItem + 1 )

  }

 

  public.stop = function() {   

    clearInterval( _swapperThread );

  }

 

  return public

}();

No comments: