DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Open SharePoint Link in a new Window

Posted by PANVEGA on October 12, 2008

In SharePoint when clicking on a link the windows opens in the same window as your application. However usually you still wanna have the link to your SharePoint Site. It’s definitely not the best user experience

Add the following JavaScript Code for example to your custom UserControl .ascx and add it to your Delegate Control in your master page:

Read more about Delegate Controls in my previous post

<script language=”JavaScript”>

//add an entry to the _spBodyOnLoadFunctionNames array
//so that our function will run on the pageLoad event


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++) {
//does this anchor element contain #openinnewwindow ?
if (anchors[x].outerHTML.indexOf(‘#openinnewwindow’) > 0)
//store the HTML for this anchor element
oldText = anchors[x].outerHTML;

//rewrite the URL to remove our test text and add a target instead
newText = oldText.replace(/#openinnewwindow/,'” target=”_blank’);

//write the HTML back to the browser
anchors[x].outerHTML = newText;

here is a link example:

Codeplex Feature new Link Solution:

The scope in this feature is only for SP Lists, which is not always enough flexibility setting the link for every Hyperlink you want.

Target Change with the SP Designer:

The simple way is to open the page in SharePoint Designer, convert the web part to a Data View, then right click on the field and set to option to open in a new window.

Just create your links list, open the page that has the list view on it in the Designer and right-click on the list, and click “Convert to XSLT Data View“. After it is converted, you can simply right-click on one of the links and select “Hyperlink Properties” and change the target frame according to your preference.

An other usefull Post:

It will modify ALL links that leave the current domain (except JavaScript links, don’t want to break the “Help” at the top of a SharePoint page) to open in a new window and, if you choose, will show a warning first.


spBodyOnLoadFunctionNames stores all loaded functions in an array. Compared to the document.body.onload = updateLinks which can overwrite functions.

If the spBodyOnLoadFunctionNames function did not load, the reason could be that e.g. your master page need the onload event (see below)!

<BODY onload=”javascript:if (typeof(_spBodyOnLoadWrapper) != ‘undefined’) _spBodyOnLoadWrapper();”>


If you wanna use this Script for other browser as well you have to replace:

and replace 

theLinks(i) with the theLinks[i]

var theLinks = document.links with document.getElementsbyTagName(“a”);


document.body.onload = updateLinks with _spBodyOnLoadFunctionNames.push(“updateLinks”)

one more issue

You have to set showMessage variable to 0. Otherwise if you want to expand or collapse e.g. a WebPart property field which calls the ExpGroupBy(formObj) JavaScript function, you always get the popup message on the screen and you ar not able to access the collapsed fields.

// Change “showMessage” to 0 if you don’t want the warning to pop up when navigating to external sites.
var showMessage = 1;


4 Responses to “Open SharePoint Link in a new Window”

  1. Here’s another way to do the same thing sans any ASPX programming PLUS leveraging all the arguments of the call:

  2. Indian4Ever said

    Here is another way

  3. PANVEGA said

    this post just edit a static link with the SP Designer which is the easiest way to simple change the target. However my approach is not editing the pages with the designer. Everything is within the MOSS application. In addition you can start and stop the feature whenever you want.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: