Why can’t a SharePoint list/library web part open links in a new window by default?
There are several workarounds, but here’s one easy way that seems to make sense to site owners. It adds some Javascript in an XML Web Part or Form Web Part that’s placed below the list/library web part (maybe, even at the bottom-most zone on the page, no?).
Why not use a Content Editor Web Part for the script? Well, that worked in SP2007, but to survive a migration from SP2007 to SP2010, the XML Web Part seems to be the way to go for small scripts like this, and if already in SP2010, the Form Web Part should do the trick.
The code looks for the name of the list/library on page load and adds the “target” attribute to the list web part’s table tag. So, say the list name in the current site is called “Links.” The code would look for that list name in the page source by it’s Summary attribute:
<script language="javascript" type="text/javascript">
var tbl = document.getElementsByTagName('table');
for(var i = 0; i < tbl.length; i++)
{
if(tbl[i].getAttribute("Summary") == "Links")
{
var anc = tbl[i].getElementsByTagName('a');
for(var j = 0; j < anc.length; j++)
{
anc[j].setAttribute('target', '_blank');
}
}
}
</script>
For more than one list/library on the page, the code is a bit different…
<script language="javascript" type="text/javascript">
var tbl = document.getElementsByTagName('table');
for(var i = 0; i < tbl.length; i++)
{
if(tbl[i].getAttribute("Summary") == "Links1"
|| tbl[i].getAttribute("Summary") == "Links2"
|| tbl[i].getAttribute("Summary") == "Links3")
{
var anc = tbl[i].getElementsByTagName('a');
for(var j = 0; j < anc.length; j++)
{
anc[j].setAttribute('target', '_blank');
}
}
}
</script>