Pages

Monday, October 15, 2012

JavaScript to add a global link next to Welcome menu in SharePoint 2010

Similarly to my post on adding a link next to the Site Actions menu in the Left Ribbon, you can add a link on the right ribbon on SharePoint 2010 pages, next to the Welcome menu.

ExecuteOrDelayUntilScriptLoaded(ExecuteDefaultLoad, "sp.js");

var _rightRibbonContainer = null;

function ExecuteDefaultLoad()
{

 var isWikiEdit = false;
 if ( document.forms[MSOWebPartPageFormName]._wikiPageMode != null )
 {
  if( document.forms[MSOWebPartPageFormName]._wikiPageMode.value == "Edit" )
    {
     isWikiEdit = true;
    }
  }

 var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;
 if (inDesignMode == "1" || isWikiEdit) 
 {  // page is in edit mode 
 } 
 else 
 {  
  LoadRightRibbon(); 
 } 
}

function LoadRightRibbon()
{
 
 var ribbonContainerRowRight = document.getElementById("RibbonContainer-TabRowRight");

 if( ribbonContainerRowRight != null )
 {
  if( ribbonContainerRowRight.children != null && ribbonContainerRowRight.children[2] != null )
  {
   if( ribbonContainerRowRight.children[2].children != null )
   {
    _rightRibbonContainer = document.getElementById("RibbonContainer-TabRowRight").children[2].children[0];

    if( _rightRibbonContainer != null )
    {     
     AddHelloLink();
    }
   }
  }
 }
}


function AddHelloLink()
{
 if( _rightRibbonContainer != null )
 {

  var newSpan = document.createElement("span");
  newSpan.innerHTML = '<a class="ms-menu-a" style="cursor:pointer;white-space:nowrap;"    href="javascript:;" title="Hello!" onclick="window.location=\'/sites/test123\';return false;"><span><font color=\'#8ce352\'><b><i>Hello World!</i></b></font></span></a>';
  newSpan.className = 'ms-SPLink ms-SpLinkButtonInActive ms-welcomeMenu';
  
  newSpan.onmouseover= function() {  this.className = "ms-SPLink ms-SpLinkButtonActive ms-welcomeMenu"}; 
  newSpan.onmouseout= function() {  this.className = "ms-SPLink ms-SpLinkButtonInActive ms-welcomeMenu"}; 
  

  _rightRibbonContainer.insertBefore(newSpan, _rightRibbonContainer.children[0]);
 }
}

No comments:

Post a Comment