[Info-vax] Another (Re: Useful Javascript example)

Richard Maher maher_rj at hotspamnotmail.com
Fri Nov 20 17:23:22 EST 2009


Hi,

Not that I expect it to interest the backward-looking nostalgia-junkies that
litter the shores of COV, but below is another 100 or so lines of Javascript
that I find very useful. The Event Listener registry was based on the one in
David Mark's library but if you are using a better cross-browser method to
dynamically and non-destructively attach multiple event listeners to an
element then please let me know.

Also, if you will be using Tier3Suggest (250 lines) to provide generic
predictive-text, list-of-values functionality for *any* of the Text (and
Select) fields on your HTML web-pages then you will need to include this
Tier3Common.js file.

Cheers Richard Maher

PS. Of course many of the HP/VMS high priests that you've so desperately and
vainly sought development assistance from in the past would prefer not to
empower you with such knowledge as you would become that much more difficult
to funnel into their little job-preservation schemes. "No, no! You need
Wrappers - You need BridgeWorks, Doh I mean WSIT - You need [g]SOAP - IFDL
is not that bad. Stay here and feed us with your license fees; there be
dragons out there!" Tell me again why Digital went broke?

/**
 * Copyright (c) Richard Maher. All rights reserved.
 *
 * Bits & Bobs that help Tier3 Client Access.
 *
 * @author    Richard Maher
 * @version   1.0
 *
 */
    var listenerRegistry = function() {

       var globalObj       = this,
           actionListeners = {},
           targetId        = 1,
           listenerId      = 1;

       var checkIn  = function(){

           if (window.addEventListener)
             return function(element, eventName, handler) {
                 element.addEventListener(eventName, handler, false);
               };

           if (window.attachEvent)
             return function(element, eventName, handler) {
                 if (!handler._fnId)
                   handler._fnId = listenerId++;

                 if (!element._evtId)
                   element._evtId = targetId++;
                 else
                   if (actionListeners[element._evtId + eventName +
handler._fnId])
                     return;

                 var normalizedHandler =
                       function() {
                         handler.call(
                                     actionListeners[element._evtId +
eventName + handler._fnId].el,
                                     globalObj.window.event);
                       };

                 actionListeners[element._evtId + eventName + handler._fnId]
=
                     {el : element, fn : normalizedHandler};

                 if (!element.attachEvent('on' + eventName,
normalizedHandler))
                   throw new Error("Unable to attach listener");
               };

           throw new Error("Unsupported Browser");
       }();

       var checkOut = function(){

           if (window.removeEventListener)
             return function(element, eventName, handler) {
                 element.removeEventListener(eventName, handler, false);
               };

           if (window.detachEvent)
             return function(element, eventName, handler) {
                 if (!element._evtId || !handler._fnId)
                    throw new Error("No such event registered on this
Object");

                 if (!actionListeners[element._evtId + eventName +
handler._fnId])
                    throw new Error("Unable to detach listener");

                 element.detachEvent('on' + eventName,
                                     actionListeners[element._evtId +
eventName + handler._fnId].fn);
                 actionListeners[element._evtId + eventName +
handler._fnId].el = null;
                 actionListeners[element._evtId + eventName +
handler._fnId].fn = null;
                 actionListeners[element._evtId + eventName + handler._fnId]
= null;
             };

           throw new Error("Unsupported Browser");
       }();

       return {
              checkIn  : checkIn,
              checkOut : checkOut
              };
    }();

    var getStyle =
        function()
        {
          if (window.getComputedStyle) {
            return function(el) {
                     return window.getComputedStyle(el, null);
                   }
          }
          return function(el){
                   return el.currentStyle;
                 }
        }();

    String.prototype.lTrim =
       function()
          {
          return (this+"").replace(/^\s+/,'');
          }

    String.prototype.rTrim =
       function()
          {
          return (this+"").replace(/\s+$/,'');
          }

    String.prototype.trim =
       function()
          {
          return (this+"").replace(/^\s+|\s+$/g,'');
          }

    String.prototype.preWS =
       function()
          {
          return (this+"").replace(/ /g,'\xA0');
          }

    function reportError(txt)
       {
       var topWindow=window.top.document.open();
       topWindow.write("<html><body style='height: 100%;'><hr><h1>Server
Communications Error</h1><hr>");
       topWindow.write("<h2>Please contact Server Support for
assistance.</h2><br />");
       topWindow.write('<p style="color:red">' + txt +
"</p></body></html>");
       topWindow.close();
       return;
       }

    function dclAST(astAddr)
       {
       setTimeout(astAddr,0);
       return;
       }





More information about the Info-vax mailing list