spacer

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

home / experts / javascript / column16


Assigning Tooltips by URL

Developer News
MicrosoftÂ’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0

By now we all know how to create useful tooltips. But our technique has several disadvantages:

  • In order to add tooltips to our pages, we must change the HTML code for the desired links.
  • In order to associate the same tooltip with multiple links, we must explicitly attach the tooltip to each of those links.

We'll solve these issues by assigning the same tooltip to all links on the page that refer to the same URL. Let's start by redesigning the interface of makeEl(). The function now needs to receive the width of the tooltip, its content, and the URL with which it matches:

makeEl(300, "A web site about creating web sites. -->
With thousands of select, annotated links, hourly -->
web news, noted columnists, and free services this -->
is webmaster <I>nirvana</I>.", "http://www.webreference.com/", -->
"http://www.webreference.com/index.html");
makeEl(300, "Let our leading experts keep you up to -->
date with the latest developments, featuring the columns -->
you need. Each is a respected author in their chosen -->
field.", "http://www.webreference.com/experts/");
makeEl(300, "A JavaScript how-to column with biweekly -->
prescriptions, that are sure to improve your JavaScript -->
health. Shows readers how to utilize the latest JavaScript -->
features.", "http://www.webreference.com/js/");

The first argument reflects the desired width, while the second one specifies the HTML code for the content of the tooltip. The third argument is the corresponding URL. If you want to associate the tooltip with multiple URLs, simply specify them as additional arguments. The first statement in the preceding script calls the makeEl() function with four arguments -- two URLs. Let's take a look at the body of the function:

function makeEl(width, code) {
  if (!style) return;
  var id = "tip" + (num++);
  var args = makeEl.arguments;
  for (var i = 2; i < args.length; i++) {
    for (var j = 0; j < document.links.length; j++) {
      if (document.links[j].href == args[i]) {
        document.links[j].tooltip = id;
        document.links[j].onmouseover = activateEl;
        document.links[j].onmouseout = clearEl;
      }
    }
  }
  var str = "<STYLE TYPE='text/css'>";
  str += "#" + id + " {";
  str += "width: " + width + ";";
  str += "}";
  str += "</STYLE>";
  str += "<DIV CLASS='tooltip' ID='" + id + "'>" + code + "</DIV>";
  document.write(str);
}

You probably noticed the new variable -- num. We define this variable in the global section of the script:

var num = 1;

We use this variable to generate on-the-fly names for our tooltips. Our naming scheme is: tip1, tip2, etc. Note that we would have used random names if it were easier, because the actual name has no significance.

The function assigns its array of arguments to a local variable named args. Thus, the third argument is args[2], the fourth one is args[3], and so forth, while the total number of arguments is args.length.

The first for statement loops through the function's arguments, starting from the third one. For each argument (the arguments are the URLs), the function loops through the links on the page, which are all members of the document.links array. If a link's URL is equal to one of the function's arguments, the current tooltip is attached to that link by:

  • setting the link's onmouseover event handler to activateEl.
  • setting the link's onmouseout event handler to clearEl.
  • adding a new property, named tooltip, to the link's object, and assigning it the name of the current tooltip (tip1, tip2, etc.).

Let's take a look at the revised activateEl() function:

function activateEl(e) {
  active = this.tooltip;
  if (NS4) document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove = checkEl;
  checkEl(e);
}

For your reference, our pevious activateEl() function was:

function activateEl(id, e) {
  if (!style) return;
  active = id;
  if (NS4) document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove = checkEl;
  checkEl(e);
}

The most obvious difference is that the new function isn't terminated if stylesheets are disabled. Since makeEl() is responsible for assigning the event handlers to tooltip-enabled links, activateEl() isn't invoked if the browser doesn't support stylesheets.

The only other difference is that our new version accepts only one argument -- the event object for Navigator. We need to pass this parameter to the checkEl() function, which uses it to extract the current position of the mouse.

Notice that the function retrieves the name of the active tooltip through this.tooltip. The keyword this reflects the link whose onmouseover event handler called the activateEl() function. As explained earlier, we added a property named tooltip to all tooltip-enabled links on the page. It holds the name of the corresponding tooltip (the ID attribute of its DIV element). Note that the following code serves as an alternative to this.tooltip:

(NS4) ? e.target.tooltip : event.srcElement.tooltip

Another interesting point is that you don't have to include any additional script at the beginning of the document, because the script itself assigns the event handlers to the desired links. For older browsers (less than 4.0x), the event handlers simply don't exist, so there's no risk.

Last but not least, we suggest that you specify a base URL for the page. For instance, the base URL for this page would be:

<BASE HREF="http://www.webreference.com/js/column16/">

This tag should be placed at the beginning of the document, in the <HEAD>...</HEAD> portion. The reason we need to specify a base URL is that a link's href property holds its full URL, even if its HREF attribute specifies a relative one. There is usually more than one way to access a server (e.g., www.webreference.com, webreference.com, webreference.internet.com), so it is best to implement a base URL.

That's all there is to it. The next section of the column lists the entire code for this technique.

http://www.internet.com



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Perl Pragma Primer · Implement Drag and Drop in Your Web Apps: Part 2 · How to Create an Ajax Autocomplete Text Field: Part 5
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
SQL Server 2005 Express Edition - Part 22 - Upgrading from Microsoft SQL Server Desktop Engine (MSDE) · Vyatta: Downgrades that Pay Off · NetMotion Brings Cross-Network Support to Wireless VoIP


Created: March 26, 1998
Revised: March 26, 1998

URL: http://www.webreference.com/js/column16/byurl.html