Sending Email with Subject Line Auto Filled

You have many pages on your site. You want to get some feed back from your visitors about the page they are visiting. So, you include your email at the bottom of the page. The visitor comes along and reads the page. They have a comment and click on the email link you have provided. Their comments are specific to the page... or perhaps vaguely referenced. You have no idea what page they are talking about.

Has this ever happened to you. It has to me! I would like to respond to the person who sent the message yet I don't quite understand his reference point. On my site I have several pages that talk about salvation. Which are they refering to? Here is a script that you can place on your page that will handle the problem. That is... if they have a javascript enabled browser with javascript turned on. If they don't, they won't see the email link.

I will show you how to apply it to one page. Then I will show you how to apply it to many pages with a simple script within a page that calls an external text file.

Important Point

The first thing that you should know about this script is that it uses the form tag plus input to create a button. When the button is clicked it fires the script. This is the way it works for all three methods. Here is the general layout of the form:

<form>
<input type="button" value="the text you want to appear on the button goes here" onClick="the code or source goes here">
</form>

This is what it looks like:

There is no code at present for the onClick event... that's next.

Inline Method

Use this method when you want to control how the email link is going to work for a single page and you want a subject line other than the title of the page where the link is located.

This is how the form will look for this method:

<form><input type="button" value="Write to me about the Inline Method" onClick="parent.location='mailto:nu_b1@hotmail.com?subject=Inline Method of Email Subject'"></form>

onClick=" is the method that is used to fire the script.
parent.location='mailto: is the way in which you call the mail program up.
nu_b1@hotmail.com this is where your email address goes.
?subject= This is how you add something to the subject line
Inline Method of Email Subject'" Place whatever text you want to appear in the subject line here.
Watch out for the double and single quote tags. They must be exact or the script will fail.
Single quotes nested (placed inside) of the double quotes.

Example: when you click on the button below the value line is on the button and the subject is entered on the email's subject line.

Embed Method

Use this script on each page that you want the subject line to reflect the title of the page. It comes in two parts: the script in the head of the document and, the form in the body

The script in the head of the document:

<script language="javascript" type="text/javascript"><--
function goingtoemail()
{
parent.location='mailto:place your email address here?subject=' +document.title
}
// --></script>

The form in the body of the document:

<form>
<input type="button" value="some text here" onClick="goingtoemail()">
</form>

What is happening here is that the onClick event in the form is calling to the script in the head of the document and asking to run the function called "goingtoemail()." This name can be almost anything you wish. It does not have to be called "goingtoemail."

After subject=' there is a space followed by a plus sign and the words document.title. This is how the title of the document is taken and placed in the subject line of the email.

Working Example:

External Method

Use this script if you want to do the same as above with many pages without all the work. You create a single external txt file and simply reference the txt file within the page.

First: create the text file. Simply open up your favorite text editor; notepad, notetab, HTML-kit; and write the following into it or copy and paste.

function goingto()
{
parent.location='mailto:your email address?subject=' +document.title
}

document.write("<form><input type='button' value='what you want to appear on the button' onClick='goingto()'></form>")

What has been done is the script in the head and the form in the body have both been placed into this text file. I named the file "emailsubjecttest.txt". When the page loads this script will embed both the javascript and the form in the document body. When the button is clicked it will call the function "goingto()."

So, how do we get the page to call on the text file to load? Simple... place this code where you want the button to appear:

<script language="javascript" type="text/javascript" src="emailsubjecttest.txt"><--
// --></script>

Of course you can name the text file anything you wish... just remember to reference the src file in the page with the same name. Below is the working example:


Hope that this helps you...

Author: Joseph Raymond

4th of April, 2001