0

I am using sp2013. I have a document library with pdf files. When I click on the pdf file it opens directly in the same window.

How can I make it possible the pdf file opens in a new window?

In Central Admin I have on my webapplication the option "Browser File Handling" set on permissive.

3

This is working:

<script src="https://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script><script>
$(document).ready(function() {
$("a[href$='.pdf']").removeAttr('onclick').attr("target","_blank");
   });
</script> 
  • this works like a charm :) – Bucki Nov 9 '16 at 11:13
  • just to add a note: unfortunately this script does not work for .pdf within a Document Library :( - shame really. Any suggestions? – Bucki Nov 18 '16 at 9:52
1

In a content editor web part source editing, you add the following script:

<script language="javascript" type="text/javascript">
    _spBodyOnLoadFunctionNames.push("SetTargetBlank('/InsertListNameHere/')");

    function SetTargetBlank(ToFindInLink)
    {
      // Get the collection of <a> tags
      var aAllLinks = document.getElementsByTagName('a');

      // For each <a> tags, 
      for(var i=0;i<aAllLinks.length;i++)
      {
         var oA = aAllLinks[i];
         var sHREF = oA.attributes["href"].value.toLowerCase();

         // If href value contains paramter
         if(sHREF.indexOf(ToFindInLink.toLowerCase()) > 0)
            oA.attributes["target"].value = "_blank";
      }
    }
</script>
  • I can see the javascript is looping into all ahrefs. When it try to set the target it gives me this error: Unable to set property 'value' of undefined or null reference – Ola Sep 11 '14 at 14:12
0

You can get this using the Content Editor Webpart with Javascript coding...

<script language="javascript" type="text/javascript">



_spBodyOnLoadFunctionNames.push("OpenPDFInNewWindow()"); 



function OpenPDFInNewWindow() 

{ 

var aAllLinks = document.links; 

for(var i=0;i<aAllLinks.length;i++) 

{ 

var oA = aAllLinks[i]; 

var sHREF = oA.href.toLowerCase(); 



if(sHREF.indexOf(".jpg") > 0 

sHREF.indexOf(".pdf") > 0) 

{ 

oA.onclick=function() 

{ 

var w=window.open(this.href,'_blank',''); 

if(w) { 

w.focus(); 

return false 

}; 

return true; 

} 

} 

} 

} </script>

Open Sharepoint document Library PDF files in New window

Also check this answer from MSDN forum.

http://social.msdn.microsoft.com/Forums/sharepoint/en-US/9e76f4f7-47d5-4bdf-8829-c024a1aed55e/pdf-files-wont-open-in-new-window-in-another-environment?forum=sharepointdevelopmentlegacy

  • I copied this code and past it in a content editor webpart on the document library page (aintranet.myCompany.com/MyListName/Forms/AllItems.aspx). It stil not working. Using IE11 compatibility mode. – Ola Sep 11 '14 at 13:37
  • IE 32 bit or 64 bit? so it did not work with/out of Compatibility mode? – Waqas Sarwar MVP Sep 11 '14 at 14:05
  • 64 bit, also without compatibility mode it is not working. Is this script also tested on sp2013? – Ola Sep 11 '14 at 14:16
0

This is the behavior you find it in IE11. IE10 it works fine.

  • Ok thats clear, but is there some workaround to make it possible? – Ola Sep 11 '14 at 13:42
0

This worked for me in SharePoint 2013 document library with grouping http://www.benramey.com/2011/04/25/opening-all-sharepoint-2010-documents-in-a-new-window/

0

This is a web app wide setting or a doc lib specific etting. In Central Admin (or through Powershell), change file handling for a web application:

https://community.colligo.com/entries/23392255-How-can-I-enable-the-option-to-open-a-file-from-SharePoint-

$web  = Get-SPWeb http://yourspweburl
#Get Document Library
$docLib = $web.lists["Your Document Library Title"]
#If you need to change it from Strict to Permissive
$docLib.BrowserFileHandling = "Permissive"
$docLib.Update()
0

The script copied from Ben's Blog: http://www.benramey.com/2011/04/25/opening-all-sharepoint-2010-documents-in-a-new-window/

    <script src="https://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script>

<script type="text/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
            );
        }
    );
    </script>

This works even with SP2013 Document Library but the only drawback is that it opens any (all) the documents in a new _blank browser.

How to make it open .PDF only in a new browser??

http://www.keremozen.com/category/sharepoint/page/5/

It is done by adding the [href*='.pdf']

$("a[onclick*='return DispEx'][target!='_blank'][href*='.pdf']")


I know the topic is over 2yrs old but this works like a charm and might come handy to somebody else who is a newbie :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.