Tip: JavaScript to enable opening links in a new window in SharePoint
by liquidpooled on Nov.13, 2008, under Office, Sharepoint Server, Windows SharePoint Services
There are times when developing in SharePoint where you will be required to modify existing links on a page to make those links open in a new window (Document Library, Links List, etc.). There are times when the original anchor tag carries an “onclick” event of “GoToLink(this); return false;”. The original “GoToLink” function will not allow for opening even links with a modified target in a new window. The following is a piece of JavaScript that will allow for overriding the original “GoToLink” function.
Note: For those links where I truly wish to override the “GoToLink” behavior, I attach a custom attribute to the anchor to be sure it is truly a link I have manipulated.
//Create a custom GoToLink function
var _origGoToLink = GoToLink;
GoToLink = function(elm) {
if (elm.href == null)
return;
var ch = elm.href.indexOf(\"?\") >= 0 ? \"&\" : \"?\";
var srcUrl = GetSource();
if (srcUrl != null && srcUrl != \"\")
srcUrl = ch + \"Source=\" + srcUrl;
var targetUrl = elm.href + srcUrl;
if (elm.getAttribute('cnWebPartsHref') != null) {
//Use window.open rather than window.location
window.open(STSPageUrlValidation(targetUrl));
}
else {
window.location = STSPageUrlValidation(targetUrl);
}
}