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);
}
}
Method GetSource() and STSPageUrlValidation(targetUrl) doesn’t exist. Could you give me some advice?
Thank you in advance
@Sini – The two methods you mention are core JS functions in SharePoint. Are you using this JS on a default web part page? A publishing page? etc. Does the page you are attempting to modify have a link list web part on it?