jQuery Random Quote Viewer

While doing some housekeeping recently on my personal site I was tinkering around with a pre-jQuery ajax enabled web part I had written to display random quotes from a SharePoint list on the site. The code was pretty ugly so I decided to adapt the web service call to use jQuery instead in line with the similar functions displays I’ve been deploying lately.

I thought I might take the time to share it as well since jQuery is still a hot topic.

Here is what the display looks like.jQuery Random Quote Viewer

The SharePoint list is a basic list with a column for the Quote and a column for the Author.

The code is currently hosted in a Content Editor Web Part (CEWP) and is pretty simple stuff. If you are going to use this, just be sure to put in the name or GUID of the list and the URL to the site’s list web service. Here is what it looks like:

<code><br /><br />    $(document).ready(function() {<br />    var URL = &quot;&quot;;  // URL ex. http://mycompany/sites/sub-site/_vti_bin/lists.asmx<br />        var listName = &quot;&quot;;  // List Name or GUID<br /><br />        var soapEnv = &quot; <br />               <br />                  <br />        &quot; + listName + &quot; <br>                         <br>                             <br>                             <br>                         <br>       <br>      &quot;;<br><br>        $.ajax({<br>            url: URL,<br>            beforeSend: function(xhr) {<br>                xhr.setRequestHeader(&quot;SOAPAction&quot;,<br>                                &quot;http://schemas.microsoft.com/sharepoint/soap/GetListItems&quot;);<br>            },<br>            type: &quot;POST&quot;,<br>            dataType: &quot;xml&quot;,<br>            data: soapEnv,<br>            complete: DisplayQuote,<br>            error: function(xhr) { $(&quot;#errorContainer&quot;).html('Error: ' + xhr.status + ' ' + xhr.statusText); },<br>            contentType: &quot;text/xml; charset=&quot;utf-8&quot;&quot;<br>        });<br>    });<br>    <br>    function DisplayQuote(xData, status)<br>  {<br>      var Output;<br>      var WikiString = &quot;http://en.wikipedia.org/wiki/Special:Search?fulltext=Search&amp;search;=&quot;<br>      var xmlDoc = xData.responseXML;      <br>      var ListItems = xmlDoc.getElementsByTagName(&quot;z:row&quot;) &#124;&#124; xmlDoc.getElementsByTagNameNS(&quot;*&quot;,&quot;row&quot;);<br>      var SelectedItem = Math.floor(Math.random()* ListItems.length);<br>      var Quote = ListItems[SelectedItem].getAttribute(&quot;ows_Quote&quot;);<br>      var Author = ListItems[SelectedItem].getAttribute(&quot;ows_Author0&quot;);      <br>      Output = &quot;<i>&quot; + Quote + &quot;</i> - &quot; + &quot;<a>&quot; + Author + &quot;</a>&quot;;<br>      $(&quot;#quoteContainer&quot;).html(Output);<br>  }<br><br /></code>

While this is simple code, it can be applied to so many different things.  You can use it to generate a slide show, show random images, etc. 

The source files are available here:

 

Related Links

Tags: ,

1 Response to "jQuery Random Quote Viewer"

Leave a Comment