Skip To Main Content | Home | Sitemap | Contact
Latest Visitors: United States's flagHong Kong's flagTurkey's flagUnited States's flagIndia's flagUnited States's flagUnited States's flagUnited States's flagGermany's flagSweden's flagMalaysia's flagMalaysia's flagUnited States's flagIsrael's flagUnited States's flagChina's flagChina's flagSweden's flagIndia's flagAustralia's flagIndonesia's flagNorway's flagVietnam's flagTurkey's flagGermany's flag
 

How can you highlight search terms on a page?

This example was written in response to a question Q_21364660 posted on Expert's Exchange.  The original question was how to highlight search terms for an ASP page, however string functions in ASP are very slow and expensive operations.  The solution proposed was to use client side (JavaScript) which is very fast.

NOTE: This page uses custom attributes on the input field and therefore has a matching custom DOCTYPE, dtd file.  Although this is permitted by the W3C 4.01 HTML specification, the W3C validator does not  read and use custom dtd files when validating pages. For more information see: Specifying Custom HTML Attributes With A Custom DOCTYPE.

Example:

The FBI's Internet Fraud Complaint Center (IFCC) reports a steady increase in complaints involving unsolicited e-mails directing consumers to a phony "customer service" Web site or directly asking for customer information.

These scams are contributing to a rise in identity theft, credit card fraud, and other Internet-based frauds. E-commerce customers, including bank customers, have fallen victim to these scams.

Phishing involves sending customers a seemingly legitimate e-mail request for account information, often under the guise of asking the customer to verify or reconfirm confidential personal information such as account numbers, social security numbers, passwords, and other sensitive information. In the e-mail, the perpetrator uses various means to convince customers that they are receiving a legitimate message from someone whom the customer may already be doing business with, such as a bank. Techniques such as a false “from” address or the use of seemingly legitimate bank logos, Web links, and graphics may be employed to mislead the customer. After gaining the customer’s trust, the perpetrator attempts to convince the customer to provide personal information and provides one or more methods for the customer to communicate that information back. For example, the e-mail might include a link to the perpetrator’s Web site that contains a form for entering personal information. Like the e-mail, the Web site is designed to trick the customer into believing it belongs to the bank. Alternatively, the e-mail might simply include an embedded form for the customer to complete.

The ultimate goal of this fraud is to use the customer information to gain unauthorized access to a customer’s bank or financial accounts or to engage in other illegal acts.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005-2010 Roderick Divilbiss">
<script language="javascript" type="text/javascript">
<!-- 
// You will need to get the search terms and
// initialize this variable.
var searchTerms='system,browser';

function highlightSearchTerms(){
  var temp = document.body.innerHTML;
  var st=searchTerms.split(',');
  var tmpBody;
  for (var idx=0;idx<st.length;idx++){
    tmpBody=temp.split(st[idx]);
    temp=tmpBody[0];
    for (var jdx=1;jdx<tmpBody.length;jdx++)
      temp+='<span class="st">'+st[idx]+'<\/span>'+tmpBody[jdx];
  }
  document.body.innerHTML=temp;
}
//-->
</script>
<style>
.st { font-weight: bold; background-color: #FFFF66 }
</style>

<title>Highlight Search Terms</title>
</head>

<body>
<p>Your page's body content here</p>
<!--This script command should appear just prior to the closing body tag-->
<script>highlightSearchTerms();</script>
</body>
</html>


This Weeks Most Popular Pages Newest Pages