Dynamically register the multi-event handler

Dynamically register the multi-event handler

Found something interesting in this page.
http://www.quirksmode.org/js/events_tradmod.html

It provides us the way to dynamically register more than one event handler like this:

var old = (element.onclick) ? element.onclick : function () {};
element.onclick = function () {old(); spyOnUser()};

The code snippet will get the previous handler if any and register it to the event together with the spyOnUser. However, if you want to apply this to multi-elements, you will find that it doesn't work for you. Considering the following code snippet:

//elements is the array which contains elementA and elementB and they have the following pre-defined event handler:
//elementA.οnclick='alert(1)';
//elementB.οnclick='alert(2)';
for (var i = 0; i < elements.length; i++) {
  var element = elements[i];
  var old = (element.onclick) ? element.onclick : function () {};
  element.onclick = function () {old(); spyOnUser()};
}

It is totally wrong if you thought that the above code snippet will change the registered event hander as:
//elementA.οnclick='alert(1);spyOnUser()';
//elementB.οnclick='alert(2);spyOnUser()';
instead, It will reqister it as
//elementA.οnclick='old();spyOnUser()';
//elementB.οnclick='old();spyOnUser()';
and the internal logic of old() is the "alert(2)"(the first invocation will create old() function with alert(1) and the second one will update it to alert(2))

In order to make it work in such scenario, You should write the code as
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
eval('var old' + i + '=' + 'element.onclick;'
+ ' element.onclick = function () {old' + i + '(); spyOnUser()}');
}

I really learn a lot from this practice; please rectify me, If there is anything wrong in my code snippet and my understanding.

You may want to try this html to get more about this.

===============================Source Code=================================

<html>
<head>
<script language="Javascript">
function spyOnUser() {
    alert('spyOnUser Method');
}

function trackFormChange() {
    for (var i = 0; i<document.bodyForm.elements.length; i++) {
     var element = document.bodyForm.elements[i];
     if (element.type == "checkbox") {
             eval('var old' + i + '=' + 'element.onclick;'
                    + ' element.onclick = function () {old' + i + '(); spyOnUser()}');
     }
    }
}
</script>
</head>

<body οnlοad="trackFormChange()">
<form id="bodyForm" name="bodyForm">
  <input type=checkbox name="testcb1" οnclick="alert('Element A')">Element A<BR>
  <input type=checkbox name="testcb2" οnclick="alert('Element B')">Element B<BR>
  <input type=checkbox name="testcb3" οnclick="alert('Element C')">Element C<BR>
</form>

</body>

</html>

==============================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值