none
New Tab for open file in Document Library not functioning for Group By RRS feed

  • Question

  • Hi,

    Im using below script, unfortunately its not working when the document library view in group by. Any solution?

    <script type="text/javascript">
    $(window).on('hashchange', function() {
       $('a.ms-listlink').each(function() {
           $(this).attr('target', '_blank');
           $(this).removeAttr('onclick');
       });

    });

    </script>

    Thank You.

    Wednesday, December 21, 2016 7:28 AM

Answers

  • Thank You for your respond.

    But It works by using below code and check open in browser in Library Advanced Settings.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    
    <script>
    
    $(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>
    • Marked as answer by Ameenah Friday, December 23, 2016 4:37 AM
    Friday, December 23, 2016 4:36 AM

All replies

  • Hi Ameenah,

    We can use JSLink to make library document open in new tab:

    SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
    
      function getBaseHtml(ctx) {
        return SPClientTemplates["_defaultTemplates"].Fields.default.all.all[ctx.CurrentFieldSchema.FieldType][ctx.BaseViewID](ctx);
      }
    
      function init() {
    
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
    
          // OnPreRender: function(ctx) { },
    
          Templates: {
    
            //   View: function(ctx) { return "LinkFilename"; },
          //     Header: function(ctx) { return ""; },
          //     Body: function(ctx) { return ""; },
          //     Group: function(ctx) { return ""; },
          //     Item: function(ctx) { return ""; },
               Fields: {
                   LinkFilename: {
                       View: function(ctx) {
                         var url = ctx.CurrentItem["FileUrl"];
                         return String.format('<a href="{0}" target="_blank">{1}</a>', url, ctx.CurrentItem["FileLeafRef"].substr(0, ctx.CurrentItem["FileLeafRef"].lastIndexOf('.'))); },
                       //EditForm: function(ctx) { return ""; },
                       //DisplayForm: function(ctx) { return ""; },
                       //NewForm: function(ctx) { return ""; }
                   }
               },
          //     Footer: function(ctx) { return ""; }
    
          },
    
          // OnPostRender: function(ctx) { },
    
          ListTemplateType: 101
    
        });
      }
    
      RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens("~siteCollection/Style Library/sads.js"), init);
      init();
    
    });

    Here is a similiar thread for your reference:

    How to always open documents in a document library in a new tab for Sharpoint 2013 Foundation

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 22, 2016 9:46 AM
  • Thank You for your respond.

    But It works by using below code and check open in browser in Library Advanced Settings.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    
    <script>
    
    $(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>
    • Marked as answer by Ameenah Friday, December 23, 2016 4:37 AM
    Friday, December 23, 2016 4:36 AM