Example 4-6. The Just-In-Time form help using Ajax
var xmlhttp; var helpItem; var helpObj = new Object(); aaManageEvent(window,"load", function() { var items = document.getElementsByTagName('label'); for (var i = 0; i < items.length; i++) { aaManageEvent(items[i],"mouseover",showHelp); aaManageEvent(items[i],"mouseout",hideHelp); } });
// retrieve the help info function showHelp(evnt) { evnt = (evnt) ? evnt : window.event; // get XMLHttpRequest object if not set if (!xmlhttp) xmlhttp = aaGetXmlHttpRequest(); if (!xmlhttp) return;
// if cached item, print existing and return if (helpObj[helpItem]) { printHelp(); return; }
// invoke help system var url = 'help.php?' + qry; xmlhttp.open('GET', url, true); xmlhttp.onreadystatechange = getHelp; xmlhttp.send(null); }
// hide help field function hideHelp() { document.getElementById('help').style.visibility="hidden"; }
// display help field with help function getHelp() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { helpObj[helpItem] = xmlhttp.responseText; printHelp(); } } function printHelp() { var help = document.getElementById('help'); help.innerHTML = helpObj[helpItem]; help.style.visibility="visible"; }
In the web page, the CSS and JavaScript files are linked in, and the form labels are wrapped in hypertext links with the individual help item attached to the URL for each, as shown in Example 4-7. The links also have an explicit title with clear information that clicking the links will open a second window, in addition to the rel="external" attribute/value.
Example 4-7. JIT help pagevalid, accessible, and active
Figure 4-1 shows the page with one of the help items displayed.
Lots of code for a simple effect, but the pieces are in place to make this into a library of JavaScript functions to provide JIT help for more than forms. JIT help is almost equivalent to another effect known as the tooltip. All that remains now is expanding the functionality to other objects and positioning the displayed material where the event occurs.
Figure 4-1.A form that uses JIT help
Please check back next week for the continuation of this article.