21

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).

1
  • I've seen that many similar tasks around here are solved using jQuery. I'm currently looking into it as it seems to ease several other tasks as well. Commented May 21, 2009 at 10:37

7 Answers 7

21

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

Good introduction here.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
"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.
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.
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.
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)
|
20

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 () {
            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.

1 Comment

+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.
4

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".

Comments

0

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]);

2 Comments

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).
Ah, I understand the problem better. Could you not return false if oldOnClick returns false?
0

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.

Comments

0

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);
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.