I have an announcement webpart on my sharepoint site. The boss wanted attachments to open in a new window so that users don’t close the attachment and get knocked from the site only to have to login again. To achieve this we settled on a policy that attachments should be in the form of PDF documents rather than word format. We contacted Microsoft who in turn developed a solution for us that consisted of a content editor web part that runs some javascript. The problem is 2 fold.
1. The javascript only works in Internet explorer. When used in other browsers the attachments open in the same window.
2. If the name of the document contains an apostrophe, IE barks a javascript error: expected “)”
How do I rewrite the code to escape the apostrophe and to function in multiple browsers?
I added the escape code as you suggested. I then tried uploading a document with an apostrophe and got the same error. when I look at the address I get:
Oh, I'm sorry, I wasn't paying enough attention. I should have looked more closely at what you were doing there. You don't want to change the HREF of a link, you simply want to add an event to these links. I was also wondering why you don't just give the links target="_blank"... (edit>> Nevermind, I see now, just went and looked at how the attachments are done).
Anyway, rather than changing the HREF of the link, here's how we'll add an onclick event to the links, instead:
Code:
<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("OpenPDFInNewWindow()");
function addEvent(obj, evt, fnc, bubble) {
//A cross-browser event attachment function
if (!bubble) {
bubble=false;
}
if (obj.addEventListener) {
obj.addEventListener(evt, fnc, bubble);
return true;
}
if (obj.attachEvent) {
return obj.attachEvent("on" + evt, fnc);
}
}
function OpenPDFInNewWindow() {
// Get the collection of <a> tags
var aAllLinks = document.getElementsByTagName('a'), i;
// For each <a> tag
for (i = 0; i < aAllLinks.length; i++) {
// Is this a pdf link?
if(aAllLinks[i].href.toLowerCase().indexOf(".pdf") > 0) {
addEvent(aAllLinks[i], "click", function () {
window.open(this.href).focus();
return false;
}, false);
}
}
}
</script>
This way, if javascript is off, or there is an unexpected error, the links will still work. Also, you won't have to worry about escaping apostrophes using this method.
Last edited by jamesbcox1980; 07-14-2011 at 03:38 PM.
I tried swapping your code for the original script I had and I do get a popup window. There is however a problem. The window displays “404 not found” and the url is http://rcda-portal/Lists/Parish%20An...ents/undefined
I thinking it has to do with the value of this.href, but I’m not sure what.
I’m going to monkey with it and see what happens. If you have any idea, please share.
function OpenPDFInNewWindow()
{
// Get the collection of <a> tags
var aAllLinks = document.getElementsByTagName('a'), i;
// For each <a> tag
for (i = 0; i < aAllLinks.length; i++)
{
// Is this a pdf link?
if(aAllLinks[i].href.toLowerCase().indexOf(".pdf") > 0)
{
aAllLinks[i].target="_blank";
}
}
}
I think it must be how your particular code is dealing with the "this" keyword. Try changing "this.href" to "aAllLinks[i].href". I'm guessing it's because we're feeding an event handler into another event handler. When I did it with window.onload instead of your page's onload handler it works fine. That's the only reason I can see that it might not be working.
I prefer the "target = '_blank'" method over a popup any day, just because modern browsers don't know when you're just doing a pop up and when you have a legitimate reason to open a popup window.
Bookmarks