3

I am using SharePoint Foundation 2010 and attempting to have my PDFs open in a new tab or window. I have been able to get this to work from document libraries by using jQuery. You can see this code below. One of the reason I am using this is to avoid the user being prompted for credentials every time they open the a PDF.

$("a[target!='_blank'][href$='.pdf']").attr("target", "_blank").removeAttr("onclick");

I also have Search Server Express 2010 setup on this server. When I try to run this same jQuery code on the results.aspx page it responds different. It is forcing a download of the PDF (the natural action) as well as opening it in a new tab/window (as I have added). I tried to add similar code to this below to prevent the downloading of the PDF. I just want it to open in a new window/tab.

 $("a[target!='_blank'][href$='.pdf']").attr("target", "_blank").removeAttr("onclick").click(function (e) {
        e.preventDefault();
    });

If anybody has any suggestions or pointers, that would be great.

1 Answer 1

0

I found this solution and it works well for me. I do not have the Search Server Express as part of my SP Foundation 2010 installation, so I can't answer whether this will have any affect with it. This is also a jQuery solution, albeit a little longer.

This code should be places before the closing tag.

<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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