views:

2154

answers:

7

I'm trying to modify all links on a page so they perform some additional work when they are clicked.

A trivial approach might be something like this:

function adaptLinks()
{
 var links = document.getElementsByTagName('a');
 for(i = 0; i != links.length; i++)
 {
  links[i].onclick = function (e)
  {
   <do some work>
   return true;
  }
 }
}

But some of the links already have an onClick handler that should be preserved. I tried the following:

function adaptLinks()
{
 var links = document.getElementsByTagName('a');
 for(i = 0; i != links.length; i++)
 {
  var oldOnClick = links[i].onclick;
  links[i].onclick = function (e)
  {
   if(oldOnClick != null && !oldOnClick())
   {
    return false;
   }
   <do some work>
   return true;
  }
 }
}

But this doesn't work because oldOnClick is only evaluated when the handler is called (it contains the value of the last link as this point).

+12  A: 

Don't assign to an event handler directly: use the subscribe model addEventListener / attachEvent instead (which also have remove pairs!).

Good introduction here.

annakata
+1 for the Quirksmode page.
outis
Or, you could use a javascript library (/me votes for jQuery) that would let you do the same with a lot more ease. But you must still go through the theory at the quirksmode page. It's quite soul-enriching for a javascript developer.
Here Be Wolves
"Ease" is relative. I can wrap this stuff in a 5 line method OR I can download a multi-K library. A library is good to have, but not at the expense of knowing how to actually do something yourself. I wouldn't anti-vote jquery, I'm just not keen on the kneejerk.
annakata
I agree that you shouldn't use a huge library just to save 10 minutes of coding / learning. But jQuery seems to help with some of my other tasks as well, so I'm currently looking into it.
racha
As well you should. I strongly advocate a developer learning libraries, I've just seen too many guys who could use jquery but not JS.
annakata
Another reason why I started to use JQuery is the problem with "this" references when using the Microsoft model (as described on Quirksmode). I need a proper "this" reference and will not waste my time coding around this issue if others have already solved it (e.g. in JQuery)
racha
Just noticed the previous comment - jquery doesn't solve your problems with `this` and it's really nothing to do with MS, and this is exactly why I have a problem with the "use jquery" answer, because then people don't really understand what's going on.
annakata
+4  A: 

Use a wrapper around addEventListener (DOM supporting browsers) or attachEvent (IE).

Note that if you ever want to store a value in a variable without overwriting the old value, you can use closures.

function chain(oldFunc, newFunc) {
  if (oldFunc) {
    return function() {
      oldFunc.call(this, arguments);
      newFunc.call(this, arguments);
    }
  } else {
    return newFunc;
  }
}

obj.method = chain(obj.method, newMethod);

In Aspect Oriented Programming, this is known as "advice".

outis
A: 

how about setting oldClick = links[i].onclick or an empty function. Like so

var oldOnClick = links[i].onclick || function() { return true; };

links[i].onclick = function (e)
   {
       if (!oldOnClick())
           return false;
       //<do some work>
       return true;
   }

Or you could use attachEvent and addEventListener as others have recommended

function addEvent(obj, type, fn) {
        if (obj.addEventListener)
                obj.addEventListener(type, fn, false);
        else if (obj.attachEvent)
                obj.attachEvent('on' + type, function() { return fn.apply(obj, [window.event]);});
}

and use like so

addEvent(links[i], 'click', [your function here]);
Russ Cam
It doesn't solve the problem that oldOnClick is evaluated when the handler is called and not when it's written. Plus: It fails to return false if the old onclick handler returns false (to stop the link from working).
racha
Ah, I understand the problem better. Could you not return false if oldOnClick returns false?
Russ Cam
A: 

Using JQuery, the following code works:

function adaptLinks(table, sortableTable)
{
 $('a[href]').click(function (e)
 {
  if(!e.isDefaultPrevented())
  {
   <do some work>
  }
 });
}

This requires using an extra library but avoids some issues that exist with addEventListener/attachEvent (like the latter's problem with this references).

There is just one pitfall: if the original onClick handler is assigned using "normal" JavaScript, the line

...
if(!e.isDefaultPrevented())
...

will always resolve to true, even in case the original handler canceled the event by returning false. To fix this, the original handler has to use JQuery as well.

racha
+4  A: 

You need to create a closure to preserve the original onclick value of each link:

<a href="#" onclick="alert('hi');return false;">Hi</a>
<a href="#" onclick="alert('there');return true;">There</a>
<script type="text/javascript">
function adaptLinks() {
    var links = document.getElementsByTagName('a');
    for (i = 0; i != links.length; i++) {
        links[i].onclick = (function (e) {
            var origOnClick = links[i].onclick;
            return function (e) {
                if (origOnClick != null && !origOnClick()) {
                    return false;
                }
                // do new onclick handling only if
                // original onclick returns true
                alert('some work');
                return true;
            }
        })();
    }
}
adaptLinks();
</script>

Note that this implementation only performs the new onclick handling if the original onclick handler returns true. That's fine if that's what you want, but keep in mind you'll have to modify the code slightly if you want to perform the new onclick handling even if the original handler returns false.

More on closures at the comp.lang.javascript FAQ and from Douglas Crockford.

Grant Wagner
+1 for the hint on closures. My example was specifically designed to execute only if the original handler didn't cancel, so your example fits nicely.
racha
A: 

This function should be usable (event listeners approach):

function addEventListener(element, eventType, eventHandler, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, eventHandler, useCapture);
        return true;
    } else if (element.attachEvent) {
        return element.attachEvent('on' + eventType, eventHandler);
    }
    element['on' + eventType] = eventHandler;
}

or you can save some more code adding this function (if you need to add the same event listener to many elements):

function addClickListener(element) {
    addEventListener(element, 'click', clickHandler, false);
}
A: 

I had problems with overloading in the simple way - this page was a great resource http://www.quirksmode.org/js/events_advanced.html

Julian Kuiters