The Mailtool: Writing the Tool's Script, Part I - Doc JavaScript

The Mailtool

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:

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: