spacer

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

home / experts / javascript / column70


The Mailtool

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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