fredag den 7. maj 2010

Pop Up New Window Using the Out-of-the-box Links List

SharePoint "links lists" are very handy. The only problem is that out-of-the-box, the links take over the browser window. While desirable for some navigation tasks, it is not always optimal.

Sometimes I use a Content Editor Web Part to make custom links, but these are not permission sensitive the way SharePoin Lists can be.

There is a 3rd party web part add-on called "Windows Links" for WSS3/MOSS2007 (see this article) that works just fine, but it requires an installation of the web part. This can (and probably will) cause issues during an upgrade to the next version of SharePoint.

The basic idea is that Link List is added to a Web Part Zone on a site page or web part page. Then a Content Editor Web Part is added (anywhere) onto the same page.

The code is entered into a Content Editor Web Part using the "Source Editor" (not the Rich Text Editor), as pictured below.

Copy/Paste code below:

<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>

The Content Editor Web Part can be hidden if desired (look in the "Layout" section of the web part options).

And finally, when creating links in the Links List, add #openinnewwindow on the end of each link that you would like to open in a new window.


 

Ingen kommentarer: