We wanted an easy solution to open every document in a SharePoint from a document library in a new window. This way, the user stays on the page he is originally on and the document (PDF, Word doc, Excel files–whatever) will open in a blank window.
There are two types links on page. One is that links added in the content as html. The other type is that links in a document library and displayed on page via DVWP.
Here is how to open the first type of links in new window:
1. Attach a javascript file in the master page or just add the javacript in the header section
2. Here is the javascript:
//add an entry to the _spBodyOnLoadFunctionNames array
//so that our function will run on the pageLoad event
_spBodyOnLoadFunctionNames.push("rewriteLinksunction rewriteLinks() {
//create an array to store all the anchor elements in the page
var anchors = document.getElementsByTagName("a");
//loop through the array
for (var x=0; x<anchors.length; x++) {
//set the target attribute to be blank to open in new window
anchors[x].setAttribute('target', '_blank');
}
}
3. In IE, open Internet Options. General tab -> Tabs -> Settings ->When a pop-up is encountered -> Always open pop-ups in a new tab
Here is for the second type:
1. Attach a javascript file in the master page or just add the javacript in the header section
2. Here is the javascript:
$(document).ready(
function
()
{
// 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']"
)
.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
);
}
);
No comments:
Post a Comment