How to open files/documents in SharePoint document library is new tab
As you know documents in SharePoint document library (images, pdf files etc) always opens in the same page and it leaves you from the SharePoint context. You can not go back to SharePoint from that page unless you press the back button of your browser.
Here is a fix/workaround for this problem. Put below code to a js file and refer this file in your master page.
try {
//Below code is for opening document library files(pdf,jpg) in new tab
// has to be on an interval for grouped doc libraries
// where the actual links are loaded only once a group
// is expanded
setInterval(
function () {
$("a[onclick*='return DispEx'][target!='_blank'][aria-label$='pdf File'],a[onclick*='return DispEx'][target!='_blank'][aria-label$='jpg File']")
.attr("target", "_blank")
.removeAttr("onclick");
// document type icons
$("td.ms-vb-icon>img[onclick]:not([documentUrl])")
.click(function (e) {
window.open($(this).attr("documentUrl"), "_blank");
e.stopPropagation();
e.preventDefault();
return false;
})
.each(function () {
$(this).attr(
"documentUrl",
$.trim(String($(this).attr("onclick"))
.split("=")[1]
.replace(/["'{}]/g, "")
.split(";")[0])
);
this.onclick = null;
});
},
500
);
} catch (e) {
}
As you can see the above code works for all the document libraries and only for below file type:
- jpg
Since the ms-office file always opens in the office application or it downloads to the machine, we do not need to add them here. You can modify this code for png or any other types.
Comments
Post a Comment