The Mailtool: Writing the Tool's Script, Part II - Doc JavaScript
The Mailtool
Writing the Tool's Script, Part II
The main function of the mailto: URL tool is createMailto()
. The function accepts three parameters: sourceForm
(the name of the form), targetField
(the name of the target field for the mailto: URL), and urlType
(either url
or anchor
). The function createMailto()
assembles the mailto: URL from the form fields, as filled by the user. We first find out the values entered by the user:
var to = sourceForm.to.value;
var cc = sourceForm.cc.value;
var bcc = sourceForm.bcc.value;
var subject = sourceForm.subject.value;
var body = sourceForm.body.value;
var linkText= sourceForm.linkText.value;
The linkText
variable denotes the text that will be shown on the assembled link. This field is optional and we assign our default if left empty:
if (linkText == "") {
linkText = "Link Text";
}
The function iterates through the message's fields and adds them one by one, following a proper delimiter:
var urltext = "";
// IF THE VALUE IS SET, INCLUDE IT IN THE URL
if (to != "") {
urltext += to;
}
else {
alert("Sorry, you must fill out the 'To' field");
sourceForm.to.focus();
return(1);
}
if (cc != "") {
urltext = addDelimiter(urltext);
urltext += "CC=" + cc;
}
if (bcc != "") {
urltext = addDelimiter(urltext);
urltext += "BCC=" + bcc;
}
if (subject != "") {
urltext = addDelimiter(urltext);
urltext += "Subject=" + escape(subject);
}
if (body != "") {
urltext = addDelimiter(urltext);
urltext += "Body=" + escape(body);
}
if (urlType == "url") {
urltext = "mailto:" + urltext;
}
else {
urltext = "<A HREF=\"mailto:" + urltext + "\">" + linkText + "</A>";
}
Notice how we escape
the subject
and the body
parameters. This is to allow for ampersands and question marks to be legal characters in the mail message's body or subject. What's left to be done now is writing the URL text in the mailto: field, and setting focus and selection on it:
// PUT THE NEW URL IN THE FORM FIELD
targetField.value = urltext;
// GIVE THE FIELD FOCUS AND HIGHLIGHT THE TEXT --
// TO FACILITATE EASY COPYING AND PASTING OF THE NEW URL
targetField.focus();
targetField.select();
return(1);
Next: How to write a script for generating mailto: URLs
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: October 9, 2000
Revised: October 9, 2000
URL: https://www.webreference.com/js/column70/6.html