Low impact text changing in SharePoint with jQuery

Mike Smith over at Tech Training Notes had a nice simple post a few weeks back on changing the default text displayed at the bottom of stock web parts in SharePoint. For example discussion boards show this text when it's blank:

"There are no items to show in this view of the "Test" discussion board. To create a new item, click "New" above. 

That's great but what if the "New" link isn't avaiable (if you turn off the full toolbar and put a discussion list on a page this would happen). Or what if you don't like calling it a discussion board (for various reasons) and want to say forum. Or want to provide additional information.

His solution is a little bit of JavaScript and then pushing his function into an array _spBodyOnLoadFunctionNames (which is a list of scripts SharePoint will run when the page loads). Here's the original version:

   1:  <script type="text/javascript">
   2:   
   3:  function ChangeDiscussionMessage()
   4:  {  
   5:      var a = document.getElementsByTagName("TD")  
   6:      for (var i=0;i<a.length;i++)  
   7:      {    
   8:          if (a[i].className=="ms-vb")    
   9:          {      
  10:              if (a[i].innerText.indexOf("There are no items to show in this view")>-1)      
  11:              {         
  12:                  a[i].innerHTML = "There are no active discussions";      
  13:              }    
  14:          }  
  15:      }
  16:  }
  17:   
  18:  _spBodyOnLoadFunctionNames.push("ChangeDiscussionMessage") 
  19:   
  20:  </script>

Here's a more simplified jQuery version:

   1:  <script type="text/javascript">
   2:   
   3:  $(document).ready(function(){
   4:      $("td .ms-vb:contains('There are no items to show in')").text('There are no active discussions');
   5:  });
   6:   
   7:  </script>

P.S. I'm still trying to find time to write up the blog post from this last weekend's code camp and will post the SharePoint developer resources soon!

Published Wednesday, September 23, 2009 7:08 AM by Bil Simser
Filed under: ,

Comments

# re: Low impact text changing in SharePoint with jQuery

Wednesday, September 23, 2009 10:03 PM by Chris Chapman

jQuery is fast-becoming, oh hell - it already is - the band aid for fixing up SharePoint without having to delve into the master pages, layout or custom web parts.  In my travels, I've accomplished more with a few lines of jQuery than dozens of lines of XML, ASP.NET and c#...

# First Look at Google Wave; Elop Isn't Afraid of Google; Is the Microsoft Tablet Real?

Thursday, September 24, 2009 8:45 AM by SharePoint Daily

Top News Stories SharePoint Usage Reporting and the Bottom Line (SearchWinIT) Usage reports speak directly

# re: Low impact text changing in SharePoint with jQuery

Friday, September 25, 2009 12:21 PM by Alex Angas

Hi Bil,

Regarding your second code snippet, I've had to perform this same task in jQuery. My target browser is IE7 (was IE6 then) so I was concerned about using something as general as "contains" to find DOM elements due to slow performance. Instead I went with something that is faster but harder to read and understand. Reconsidering this as simplicity leads to maintainability, I asked a question on Stack Overflow for guidance. The answers may help some of your readers: stackoverflow.com/.../should-i-care-about-javascript-engine-speed-when-using-jquery.

Thanks for your post,

Alex Angas.

# re: Low impact text changing in SharePoint with jQuery

Thursday, October 20, 2011 10:05 AM by Kishore

Thank you for JQuery solution, actually for some reason javascript code is not working in my site, I was using JQuery for other stuff, so JQuery worked for me.

Thanks again

Leave a Comment

(required) 
(required) 
(optional)
(required)