There are two types links on page. One is that links added in the content as html. The other type is that links in a document library and displayed on page via DVWP.

Here is how to open the first type of links in new window:

1. Attach a javascript file in the master page or just add the javacript in the header section

2. Here is the javascript:

//add an entry to the _spBodyOnLoadFunctionNames array 
//so that our function will run on the pageLoad event 
_spBodyOnLoadFunctionNames.push("rewriteLinks");
function rewriteLinks() {  
//create an array to store all the anchor elements in the page   
var anchors = document.getElementsByTagName("a");   
//loop through the array   
for (var x=0; x<anchors.length; x++) {    
//set the target attribute to be blank to open in new window    
anchors[x].setAttribute('target', '_blank');   
} 
}

3. In IE, open Internet Options. General tab -> Tabs -> Settings ->When a pop-up is encountered -> Always open pop-ups in a new tab

Here is for the second type:

1. Attach a javascript file in the master page or just add the javacript in the header section

2. Here is the javascript:

$(document).ready(
  function ()
  {
    // has to be on an interval for grouped doc libraries
    // where the actual links are loaded only once a group
    // is expanded
    setInterval(
      function ()
      {
        $("a[onclick*='return DispEx'][target!='_blank']")
          .attr("target", "_blank")
          .removeAttr("onclick");

        // document type icons
        $("td.ms-vb-icon>img[onclick]:not([documentUrl])")
          .click(function (e)
          {
            window.open($(this).attr("documentUrl"), "_blank");
            e.stopPropagation();
            e.preventDefault();
            return false;
          })
          .each(function ()
          {
            $(this).attr(
            "documentUrl",
            $.trim(String($(this).attr("onclick"))
              .split("=")[1]
              .replace(/["'{}]/g, "")
              .split(";")[0])
            );
            this.onclick = null;
          });
      },
      500
    );
  }
);

Reference:
http://www.benramey.com/2011/04/25/opening-all-sharepoint-2010-documents-in-a-new-window/
http://www.sharepointkings.com/2008/07/open-documents-in-new-window.html

2 thoughts on “JavaScript – Open links on page in new window/tab in SharePoint 2010

  1. Thanks! But simple and easiest solution is: Create a Site column of “Hyperlink with formatting and constraints for publishing” type and add it in your list, which enables you to specify “Open in New Window” Option!

    There are few more possible solutions, including JavaScript, JQuery, SharePoint Designer, List Schema Edit to make SharePoint Link list open in new window at SharePointDiary.com –

    http://www.sharepointdiary.com/2011/02/sharepoint-link-list-open-in-new-window_21.html#ixzz2aWtiNarl

Leave a comment