Grab Keyword Phrase

How can I grab the keyword phrase that someone used to enter the website and then pass that through a form? I would need to know for search engines and Google AdWords as well. Does anyone know how to do this?

This script does it by parsing the document.referrer property.

Thank you so much for the quick reply! I see that this will highlight words on the page but how can I grab the search term that they used to enter the website?

Here are the functions that that script uses:

  googleSearchHighlight: function() {
    var ref = document.referrer;
    if (ref.indexOf('?') == -1) return;
    var qs = ref.substr(ref.indexOf('?')+1);
    var qsa = qs.split('&');
    for (var i=0;i<qsa.length;i++) {
	    var qsip = qsa[i].split('=');
      if (qsip.length == 1) continue;
      if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google, p= for Yahoo
		    var wordstring = unescape(qsip[1].replace(/\\+/g,' '));
  process: function(wordstring) {
    searchhi.found = 0;
    var words = wordstring.split(/\\s+/);
    for (w=0;w<words.length;w++) {
    if (searchhi.found === 0) {

It looks like it works like this:

  1. It gets the document.referrer
  2. It looks at that refferer for the query string (everything after the “?”)
  3. It searches the query string for either a “q=” (which is what Google uses) or a “p=” (which is what Yahoo uses) (…according to them; I thought I read somewhere that Yahoo doesn’t actually do searches anymore – does it through Bing, or something; whatever, doesn’t matter, you can accomodate as many search engines as you want)
  4. It splits that q/p value on …white space? That seems undependable, but whatever; anyway, the resulting array is theoretically the different words the person was using to search
  5. It searches the document.body for any occurrence of each word

The Aristocrats!

That is awesome! Thank you so much for taking the time to put this together. I know how to save this and link it on the page for the js file, but how do you then call the actual keyword phrase so I can put it in a hidden form field?

You mean, how do you actually get the words from the array once it’s generated? Or do you need that at all?

Take, for example, this google search (this would be the value of document.referrer):

If you wanted to parse that only for the search phrases, discarding everything else, you would end up with this:


Do you want each word to be in its own hidden input? Like this:

<input type="hidden" name="word_1" value="this" />
<input type="hidden" name="word_2" value="is" />
<input type="hidden" name="word_3" value="a" />
<input type="hidden" name="word_4" value="search" />

Or do you just want the whole search phrase in a single input, like this:

<input type="hidden" name="words" value="this%20is%20a%20search" />