Use search engine reference to highlight page keywords

Source: Internet
Author: User
When we use the "Web snapshot" feature of most web search engines, they will add the keyword highlighting feature to the returned snapshot page. This makes it easy for us to find the content to be searched on the webpage. The keyword highlighting of Google snapshots also integrates the word segmentation function. Different words are in different colors. However, because %! # ^ * & $! The reason is that Google's web page snapshot is very unstable, so let's add this keyword to the highlight function.

For example, if we search for the keyword Apache Asp.net in Google:

Click the result link and navigate to our own page. If we highlight the following keywords, will it be convenient for users to quickly find and locate content that interest the page?


Let's take a closer look, but the address in IE is clearly: accept? Here we use the referrer attribute of document to obtain the search engine type and keyword information. For example, in the above example, the document. referrer attribute is: http://www.google.com/search? Hl = ZH-CN & newwindow = 1 & Q = Apache + Asp.net & btng = % E6 % 90% 9C % E7 % B4 % A2 & LR = lang_zh-CN % 7clang_zh-tw. We use JavaScript to analyze the keywords of the specified search engine, and then operate the DOM object on the page to achieve the highlighted function. However, for long continuous Chinese keywords, this client highlight solution cannot provide word segmentation, which is also a weakness in the United States.

To highlight the keywords retrieved on the current pageCodeAs shown in the following figure, you can call the document. Body. onload event on the page :)

Function Friendlydisplayforsearch ()
{
VaR URL =   New Urlbuilder (document. referrer );
If (URL. m_success)
{
VaR Host = URL. m_host.tolowercase ();
If (Host. indexof ('. Google .') ! =   - 1 )
{
VaR Keywords = URL. getvalue ('Q', 'utf8 ');
If (Keywords)
{
VaRHT= NewHighlighttext ();
Ht. Execute (keywords );
}
}
Else   If (Host. indexof ('. Baidu .') ! =   - 1 )
{

}
}
}

// Due to the relationship between encoding and usage frequency, only Google search engines are currently used

Function Highlighttext (range)
{
If (Range)
{
This. M_range=Range;
}
Else
{
This. M_range=Document. Body. createTextRange ();
}
This . M_keyword = '';

This . Tostring =   Function ()
{
Return'[Class hightlighttext]';
} ;
}

Highlighttext. Prototype. Execute =   Function (Keyword)
{
If (Keyword)
{
This. M_keyword=Keyword;
}
If ( This . M_range &&   This . M_keyword)
{
VaR Separater = '';
If ( This . M_keyword.indexof ('') =   - 1 )
{
Separater='+';
}
VaR Keywords =   This . M_keyword.split (Separater );
VaR Bookmark =   This . M_range.getbookmark ();
For ( VaR I = 0 ; I < Keywords. length; ++ I)
{
VaR Keyword = Keywords [I];
If (Keyword && Keyword. Length >   1 )
{
While ( This . M_range.findtext (keywords [I])
{
This .M_range.exe ccommand ('forecolor ',' False ', 'Highlighttext ');
This .M_range.exe ccommand ('backcolor ',' False ', 'Highlight ');
This . M_range.collapse ( False );
}
This . M_range.movetobookmark (bookmark );
}
}
}
}

Other references: urlbuilder
RelatedArticle:
· Added a googletrack function for injection script.
· Added several custom functions for this blog using scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.