0

As we often see in some sites, our search keywords are already selected in the site paragraphs.

same as those selection comes if we search something in browser( e.g. in crome ctrl+F)

how this can be achieved.

thanks,

Saboor Awan
  • 1,567
  • 4
  • 24
  • 37
  • 1
    see: http://stackoverflow.com/questions/3241169/highlight-search-terms-select-only-leaf-nodes – ariel May 17 '11 at 18:21
  • You can get the referrer using PHP for example, then you can parse out the search terms if the referrer was Google. (You asked for JavaScript but I'm not completely sure what you're asking here) – pimvdb May 17 '11 at 18:22
  • I don't think this is a duplicate - the question that has been linked to gives the answer for *how to highlight terms using JavaScript/jQuery* but (as I understand it) this question is asking how to highlight the keywords when a user reaches the site by following a link from the results page of a search engine query. – no.good.at.coding May 17 '11 at 18:44

1 Answers1

2

There are two steps to this:

  1. Make use of the referer property - in JavaScript, you can get it from document.referrer (also see MDC page) (note the difference in spelling from the HTTP Header)- to see if the user arrived at your page from a search engine and then extract the search keywords.
  2. Use regex to highlight keywords on the page

If you'd rather not work on this yourself, here are some pre-baked scripts you might be able to make use of:

Plain old JavaScript

jQuery

no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51