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.


This is working:

<script src="https://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script><script>
$(document).ready(function() {
  • 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

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

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

    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";
  • 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

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

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


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) 




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

if(w) { 


return false 


return true; 




} </script>

Open Sharepoint document Library PDF files in New window

Also check this answer from MSDN forum.


  • 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

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

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/


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:


$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"

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">
        function () {
            // has to be on an interval for grouped doc libraries
            // where the actual links are loaded only once a group
            // is expanded
                function () {
                    $("a[onclick*=’return DispEx’][target!=’_blank’]")
                        .attr("target", "_blank")

                    // document type icons
                        .click(function (e) {
                            window.open($(this).attr("documentUrl"), "_blank");
                            return false;
                        .each(function () {
                                    .replace(/["'{}]/g, "")
                            this.onclick = null;

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??


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.