August 22, 2000 - Mailing a Web Page | WebReference

August 22, 2000 - Mailing a Web Page

Yehuda Shiran August 22, 2000
Mailing a Web Page
Tips: August 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

You send a mail message with the mailto: URL. Open your mail application and type mailto: in your browser's Address field. A new message form will open and wait for your input. You can prefill the form by providing the subject and body parameters of the mailto: URL. You provide a parameter in the URL by prepending the parameter name with a question mark (?), followed by an equal sign (=). For example:

....?subject= ....

Notice that there should not be any spaces between the parameter name and the equal sign. To send the current page's URL in a mail message, we just assemble the current location.href in the body part:

&body= ...location.href...

To make the message even more attractive, we decorate both its subject and its body with the current page's title, document.title. The following mailpage() function assembles the mail message and posts it to the browser's Address field:

<script language="javascript">
function mailpage()
{
  mail_str = "mailto:?subject= Doc JavaScript: " + document.title;
  mail_str += "&body= I recommend Doc JavaScript's (docjavascript.com) tip -- " + document.title;
  mail_str += ". You should check this out at, " + location.href; 
  location.href = mail_str;
}
</script>

Somewhere on the page you need to provide a link that calls the above function, like this:

<A HREF = "javascript:mailpage()">E-mail This Page</A> E-mail This Page

This tip has been contributed by Paul Hansford.