1. Technology

Your suggestion is on its way!

An email with a link to:

http://javascript.about.com/library/blformref.htm

was emailed to:

Thanks for sharing About.com with others!

Passing the Referrer in a Form

Join the Discussion

Questions? Comments?

One piece of information that you might like to pass along with the information that your visitors enter into a form on your site (particularly if you have lots of pages that link to the form page) is which page that they were on before they came to the form. One example of where this would be particularly useful if the form were there to allow people to report errors found in the content of your site.

It is actually quite easy to add a hidden field to our form to hold this information and then use Javascript to set the value of the field appropriately.

Let's start by adding a field to our form. All we need to do is to define a new hidden field and give it an id (I'll use 'referrer').

<input type="hidden" value="" id="referrer" />

Now all that we need to do is to add a small piece of Javascript into the head of our page to load this new hidden field. We'll call it in the onload function after the page has finished loading to make sure that the field exists before we try to assign a value to it.

function start() {
var ref = document.getElementById('referrer');
ref.value = document.referrer;
}
onload = start;

Now provided that your visitor has Javascript enabled in their browser and has not disabled the document.referrer header in their browser or firewall the referring page will be passed along with the other fields when the form is submitted. If they do have either Javascript or that header disabled (or they go directly to your page by typing the address into their browser bar, or they are using IE and were last on a secure web page) then the hidden field will still contain blank when it is passed.

See More About

©2014 About.com. All rights reserved.