Skip to main content

Open Document Library documents in a new tab

Open Document Library documents in a new tab


Set this scritp on the page or master page.

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

Comments