【问题标题】:Sharepoint 2013 - Open search result in new window/tabSharepoint 2013 - 在新窗口/标签中打开搜索结果
【发布时间】:2015-05-08 21:57:44
【问题描述】:

好的,所以我已经为这个问题苦苦挣扎了几个小时...... 我需要修改搜索结果中的链接,以便它们在新窗口/标签中打开。

具体来说,它是链接到“场外”点击的搜索结果。 我创建了 Item_WebPage.html 的副本,但我无法让它工作。

我猜是某种异步负载搞砸了。

我的js代码如下:

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

但是,“锚点”始终为“0”。 我可以使用“sharepoint-document-ready-as-h*ll”功能吗? 我的猜测是我的问题是在我运行代码之前并非所有内容都加载到 DOM 中......

【问题讨论】:

  • 我可能应该补充一点,我是显示模板的新手,如果我对“Item_WebPage.html”方式不满意,请让我现在轻轻地 :)

标签: sharepoint-2013 display-templates


【解决方案1】:

自定义显示模板是更改搜索结果行为的好方法。我使用了 JQuery 代码来确保外部链接总是在新窗口中加载:

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

【讨论】:

    【解决方案2】:

    太长了,你可能已经解决了。

    我正在寻找相同的内容并将 - target="_blank" 添加到自定义显示模板中的锚标记

    【讨论】:

      【解决方案3】:

      在 SharePoint Online 中,这对我来说非常有效,但适用于所有结果。

      使用 SharePoint Designer,从 _catalogs/masterpage/display templates/search 中更新 Item_CommonItem_Body.html 显示模板。

      搜索以下行: var titleHtml = String.format('&lt;a clicktype="{0}" id="{1}" href="{2}" class="ms-srch-item-link" title="{3}" onfocus="{4}" {5} &gt;{6}&lt;/a&gt;',

      并将其替换为: var titleHtml = String.format('&lt;a clicktype="{0}" id="{1}" href="{2}" class="ms-srch-item-link" title="{3}" onfocus="{4}" {5} target="_blank"&gt;{6}&lt;/a&gt;',

      保存更改并从浏览器母版页库中发布主要版本。当您现在搜索时,结果应该会在新选项卡中打开,尽管您可能需要再次加载页面缓存 (CTRL+F5)。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-14
        • 2017-04-07
        • 1970-01-01
        • 2014-11-05
        • 1970-01-01
        • 2011-07-05
        相关资源
        最近更新 更多