SharePoint中的搜索结果默认是不高亮显示搜索关键字的,最近我看到老外有一篇文章介绍如何实现这个
为了防止文章无法打开,就把它转载到这里一下。
原文地址:http://msd2d.com/newsletter_tip.aspx?section=sharepoint&id=c89e3424-d7e6-4f79-9c89-be219513357b
Submitted By: Igor Goldshtaub
Posted On: 4/10/2006 |
Hi all,
Highlighting my searched text in the search results page is one of the basic features I would expect from a search engine. Unfortunately this is not the case in SharePoint 2003.
Many of my customers requested a basic or extended support for Search results highlighting.
Extended support includes some extra functionality such as highlighting the text inside documents opened from the search results page. This will require a more complicated solution; you may find a link to a nice add-on that does the trick (both WSS and SPS) at the end of this article.
If all you are interested for now is in highlighting the searched text in the WSS search results page, you should be able to complete this by adding some JavaScript code to the searchresults.aspx page located in the SharePoint layouts folder.
This example adds this functionality to an English SharePoint search result page. In case you are not using English please change any reference to "1033" to your own language's LCID number.
Solution Description:
ý First open the searchresults.aspx page to edit.
o Open "C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\TEMPLATE\LAYOUTS\1033" folder.
o Locate the "searchresults.aspx" file
Open it using notepad or any other text editor.
ý Then, you should insert a JavaScript code block into the page that will run and do the highlighting work for you:
o Open a script block before the </body> tag:
<script language="javascript">
//Your code goes here…
</script>
Copy the attached source code into the JavaScript block:
ý Now, all you have to do is save and close the page, sit back and wait for compliments from your boss/customer/colleagues.
And don’t tell me taking 5 minutes to do it wasn’t worth it!
For conclusion, from now on – when you are searching items in a WSS site using nothing but the normal WSS search page, the results should look like this:
Note: If you're looking for a more complete solution that support both SPS/WSS searches, and also highlights the searched text inside documents (MS WORD, HTML, .TXT, PDF), you should have a look at this SharePoint add-on we have developed at KWizCom:
http://www.kwizcom.com/ProductPage.asp?ProductID=28&ProductSubNodeID=79
这是附件代码:
var strSearchTerm = document.forms[ " frmSearch " ].elements[ " SearchString " ].value;
HighlightSearchTerm(strSearchTerm);
function HighlightSearchTerm(strSearchTerm)
{
var cellTables = document.all.tags("table");
var strSearchTerm = strSearchTerm.toLowerCase();
if(strSearchTerm == "")
return;
for(var ii = 0; ii < cellTables.length; ii++)
{
if((cellTables[ii].all["frmSearch"] != null) || (cellTables[ii].className != ""))
continue;
var cellTags = cellTables[ii].all.tags("td");
for(var iii = 0; iii < cellTags.length; iii++)
{
if(cellTags[iii].className != "ms-vb")
continue;
var strBodyText = cellTags[iii].innerHTML;
var strText = strBodyText.toLowerCase();
var i = -1;
var strNewText = "";
while (strBodyText.length > 0)
{
i = strText.indexOf(strSearchTerm, i+1);
if (i < 0)
{
strNewText += strBodyText;
strBodyText = "";
}
else
{
if (strBodyText.lastIndexOf(">", i) >= strBodyText.lastIndexOf("<", i))
{
if (strText.lastIndexOf("/script>", i) >= strText.lastIndexOf("<script", i))
{
strNewText += strBodyText.substring(0, i) + "<B style='COLOR: black; BACKGROUND-COLOR: #ffff66'>" + strBodyText.substr(i, strSearchTerm.length) + "</B>";
strBodyText = strBodyText.substr(i + strSearchTerm.length);
strText = strBodyText.toLowerCase();
i = -1;
}
}
}
}
cellTags[iii].innerHTML = strNewText;
}
}
}