using jQuery to activate default SharePoint tab

So, after my previous post on using JavaScript to activate a default tab, I finally found I really wanted a reliable, reusable Jquery script for activating the default tab. Here’s my script:

<script type=”text/javascript”>//<![CDATA[ 
  //test the URL, if it's not already showing a default tab,
  //and we like the current list, then show default tab:
$(document).ready(function () {
  if(locationTest()){
    setTimeout("setTabDefault('Ross.CustomOptions');",150);
  }
});
function locationTest(){
 if(window.location.href.indexOf('InitialTabId') > 0){
   return false; //do not activate tab, if another one was specified in the URL
 }
  //if these strings are detected in the URL, we want to activate tab
 var testStrings = ["GroceryList","Style%20Library"];

 for (var x=0;x<testStrings.length;x++)
 {
  if (window.location.href.indexOf(testStrings[x]) > 0){
    return true;
  }
 }
 return false;
}

function setTabDefault(tabid){
   $(“li[id=" + tabid + ".Tab-title] a span.ms-cui-tt-span”).trigger(‘click’);
}

</script>

Leave a Reply