Writing the Tool's Script, Part I
The script part of the mailto: URL tool includes four functions: createMailto(), testMailto(), viewMailto(), and addDelimiter(). Let's cover the short ones first. The viewMailto() function uses alert to display the URL:
function viewMailto(mailtoText) {
alert("URL:\n\n" + mailtoText);
}
The addDelimiter() function adds a question mark (?) before the address name/value pair, and an ampersand (&) before any other name/value pair. We decide which delimiter to put after we search the URL text for a question mark. If it exists, we add an ampersand. Otherwise, we add a question mark:
// ADD THE "?" OR "&" NAME/VALUE SEPARATOR CHARACTER
function addDelimiter(inputString) {
var inString = inputString;
// IF '?' NOT FOUND, THEN THIS IS THE FIRST NAME/VALUE PAIR
if (inString.indexOf("?") == -1) {
inString += "?";
}
// ELSE IT'S A SUBSEQUENT NAME/VALUE PAIR, SO ADD THE '&' CHARACTER
else {
inString += "&";
}
return inString;
}
The function testMailto() simulates what's happening when you click a mailto: URL. The slight complication stems from the need to extract the URL from a complete anchor element, if a link is passed to the function instead of just a URL. We test the URL by just assigning it to window.location:
// TEST THE MAILTO URL -- ASSIGN THE URL TO THE DOCUMENT LOCATION
// TO POP UP THE MESSAGE WINDOW
function testMailto(loc) {
var doc = loc;
// IF MAILTO URL IS EMBEDDED IN AN ANCHOR TAG
if (doc.indexOf("HREF=") != -1) {
// EXTRACT THE MAILTO URL FROM THE ANCHOR TAG
var doc = doc.substring(doc.indexOf("HREF=")+6,
doc.indexOf(">")-1);
}
// ASSIGN THE MAILTO URL TO THE DOCUMENT
// (THIS WILL POP UP A MAIL WINDOW)
window.location = doc;
}
         
Next: How to write the script part of the mailto: URL tool, part II
|