I have a document library in SharePoint 2010 and the library will hold different types of files: Word, Excel, PDF, HTML...
Most of the file types open in an application. Those that don't, like .htm newsletters, open in the same window. The Sharepoint document library links to the files, but does not allow the setting of the target property.
I'd like to set this programmatically onload.
I've taken a stab at writing the code:
for(var i = 0, l=document.links.length; i<l; i++) {
var id = document.links[i].href;
var idl = id.length;
if(idl >=7 ){
var lastfour = id.substr(id.length - 4);
var lastfive = id.substr(id.length - 5);
if (lastfour == ".pdf"){
//alert(document.links[i].href);
document.links[i].setAttribute('target', '_blank');
document.links[i].setAttribute('onfocus', 'return flase;');
}
if (lastfour == ".htm"){
//alert(document.links[i].href);
document.links[i].setAttribute('target', '_blank');
document.links[i].setAttribute('onfocus', 'return false;');
}
if (lastfive == ".html"){
//alert(document.links[i].href);
document.links[i].setAttribute('target', '_blank');
document.links[i].setAttribute('onfocus', 'return false;');
}
}
}
This works because it causes the link to open in a new window, but also opens it in the main window. After further research, I found out that SharePoint does some wacky things with the links:
<a onfocus="OnLink(this)" href="/Diocesan/2017 Diocesan Special Collection Calendar.pdf" onmousedown="return VerifyHref(this,event,'0','PdfFile.OpenDocuments','')" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','PdfFile.OpenDocuments','','','','1210','0','0','0x400001f07fbf1bff','','')">2017 Diocesan Special Collection Calendar</a>
I think my problem is due to the onfocus attribute being set or maybe the onclick. I'm not sure what is happening. Should I try setting onmousedown, onclick & onfocus = ""?