PANVEGA’s Blog

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

_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++) {
//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;
}
}
}
</script>

here is a link example:

http://moss.litwareinc.com/Lists/CustomType/MyView.aspx#openinnewwindow

Codeplex Feature new Link Solution: http://www.codeplex.com/features

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.

http://www.sharepointblogs.com/dustin/archive/2006/02/03/supergeek-tip-open-off-site-links-in-a-new-window.aspx

Information:

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();”>

Caution!!!

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”);

replace

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;


Advertisements

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 window.open call:

    http://www.kevinperkins.com/tips/Lists/Posts/Post.aspx?ID=3

  2. Indian4Ever said

    Here is another way
    http://vivekthangaswamy.blogspot.com/2007/04/moss-2007-onlick-link-open-in-separate.html

  3. PANVEGA said

    this post http://vivekthangaswamy.blogspot.com/2007/04/moss-2007-onlick-link-open-in-separate.html 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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: