This is harder than it sounds, mostly because of the way SharePoint renders it pages and links within. If you are displaying a links list on a page and it is GROUPED using metadata, then you are going to have a bit of a tough time accessing those links using Javascript. The key is to use SharePoint’s version of the
$( document ).ready “trigger” called _spBodyOnLoadFunctionNames.push
Below is some code I used which worked nicely. There is a bit of output you can remove once you see you are getting the results you want. 🙂 Just add into a CEWP on the same page (or MP ref).Add you script tags to ref jQuery lib and surround JS below:
function sharePointReady()
{
updateLinks();
//Hook into SharePoint event to fix all links in group-by tables.
var oldExpand = ProcessImn;
ProcessImn = function(){
var results = oldExpand.apply(this, arguments);
updateLinks();
return results;
}
}
function updateLinks()
{
var url = ‘://’ + window.location.hostname;
$(“a”).each(function() {
var link = this; // assign the link object to another variable for easier managability
var linkHref = link.href.toLowerCase(); // lower case it
console.log(url);
console.log(linkHref);
if(linkHref.indexOf(url) < 0 && linkHref.indexOf(‘javascript:’) 0){ // check to see if this is a PDF
link.target = ‘_blank’; // change the target to be in the new window
$(link).removeAttr(“onclick”); //remove the SP click event
}
if(linkHref.indexOf(‘/forms/’) > 0 && linkHref.indexOf(‘).aspx’) > 0){
//check for links in the forms library
link.target = ‘_blank’; // change the target to be in the new window
$(link).removeAttr(“onclick”); //remove the SP click event
}
});
}
//this is the SharePoint preferred alternative of $(document).ready
_spBodyOnLoadFunctionNames.push(“sharePointReady”);