spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column70


The Mailtool

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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

http://www.internet.com


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: October 9, 2000
Revised: October 9, 2000

URL: http://www.webreference.com/js/column70/5.html