1

Ok, so I've been struggling with this problem for a few hours now... I need to modify the links in the search results, so that they open in a new window/tab.

Specifically it's the search results that links to a "off-site"-hit. I've created a copy of Item_WebPage.html, but I just can't get it to work.

I guess that there are some kind of async loads that screws it all up.

My js-code is as follows:

var anchors = document.getElementsByClassName('ms-srch-item-link');
    for (var i = 0; i < anchors.length; i++) {
            anchors[i].setAttribute("target", "_blank");
    }
}

However, "anchors" always is "0". Is there a "sharepoint-document-ready-as-h*ll"-function I can use? My guess is that my problem is that not all content is loaded into the DOM before I run my code...

pydis
  • 11
  • 1
  • 2
  • I should probably add that I'm new to Display Templates, If i'm way off with the "Item_WebPage.html"-way, please let me now gently :) – pydis May 08 '15 at 22:00

3 Answers3

0

A custom display template is a good way of changing the behavior of the search results. I've used JQuery code to make sure that external links always load in a new window :

<script type="text/javascript">

        if (window.jQuery) {
            $(window).load(function () {

                // Open external links in a new tab
                var url = '://' + window.location.hostname;
                // get the current website name, and i add :// to make sure we're looking at the right name // 
                url = url.toLowerCase(); // lowercase everything to compare apples to apples 
                $("a").each(function () {
                    var link = this; // assign the link object to another variable for easier managability 
                    var linkHref = link.href.toLowerCase(); // lower case it 
                    if (linkHref.indexOf(url) < 0 && linkHref.indexOf('javascript:') < 0) { // check to see if this A object has this domain in it and make sure it's not a javascript call 
                        link.target = '_blank'; // change the target to be in the new window 
                    } if (linkHref.indexOf('.pdf') > 0) { // check to see if this is a PDF 
                        link.target = '_blank'; // change the target to be in the new window 
                        $(link).removeAttr("onclick"); //remove the SP click event 
                    } if (linkHref.indexOf('/forms/') > 0 && linkHref.indexOf(').aspx') > 0) { //check for links in the forms library 
                        link.target = '_blank'; // change the target to be in the new window 
                        $(link).removeAttr("onclick"); //remove the SP click event 
                    }
                });


            });
        }

    </script>
Patrick Clarke
  • 101
  • 1
  • 6
0

It's way too long and you may have resolved it.

I was looking for the same and added - target="_blank" to the anchor tag in a custom display template

Nitin Rastogi
  • 1,446
  • 16
  • 30
0

In SharePoint online, this worked perfectly for me, applies to all results though.

Using SharePoint Designer, update the Item_CommonItem_Body.html Display Template from within _catalogs/masterpage/display templates/search.

Search for below line : var titleHtml = String.format('<a clicktype="{0}" id="{1}" href="{2}" class="ms-srch-item-link" title="{3}" onfocus="{4}" {5} >{6}</a>',

and replace it with : var titleHtml = String.format('<a clicktype="{0}" id="{1}" href="{2}" class="ms-srch-item-link" title="{3}" onfocus="{4}" {5} target="_blank">{6}</a>',

Save the change and, from the browser master page library, publish a major version. When you search now the results should open in new tabs although you may need to load the page cache again (CTRL+F5).

From https://social.technet.microsoft.com/Forums/ie/en-US/a7db8389-3740-4ff6-ac52-645776b29a56/search-results-in-new-tabwindow-in-sharepoint-2013