Dynamically register the multi-event handler
来源:互联网 发布:java开发web应用 编辑:程序博客网 时间:2024/06/06 23:32
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.onclick='alert(1)';
//elementB.onclick='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.onclick='alert(1);spyOnUser()';
//elementB.onclick='alert(2);spyOnUser()';
instead, It will reqister it as
//elementA.onclick='old();spyOnUser()';
//elementB.onclick='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 onload="trackFormChange()">
<form id="bodyForm" name="bodyForm">
<input type=checkbox name="testcb1" onclick="alert('Element A')">Element A<BR>
<input type=checkbox name="testcb2" onclick="alert('Element B')">Element B<BR>
<input type=checkbox name="testcb3" onclick="alert('Element C')">Element C<BR>
</form>
</body>
</html>
==============================================================
- Dynamically register the multi-event handler
- Removing event handler from inside the event handler
- You should re-bind this JavaScript event on this element if the element be dynamically updated
- Event ID 10010 - The server did not register with DCOM within the required timeout
- Register signal handler
- add event listener on elements created dynamically
- meterpreter之multi/handler
- event generator,event dispatcher,event handler.
- dynamically load more items to the ListView
- Event Handler in Laszlo
- JavaScript -- event handler
- Java pogramming : Event Handler
- nagios event handler配置
- iPhone Multi-touch Event Handling
- My own JScript Event Register mechanism
- Event Handler to trigger User process tasks when the associated Organization is updated
- Register a Custom URL Protocol Handler[转]
- android event listener and event handler
- 词法分析器完工!
- 一个.net的系统的AOP设计思路一——NHibernate和界面/对象映射层
- 我想
- Linux 内核编译 全功略
- vector 和 ArrayList
- Dynamically register the multi-event handler
- 表达能力
- IT人该怎样发展?
- 今天将blog转到CSDN来了
- 后院起火---四大名著
- 杀不完的病毒
- 5天内结贴-实现限制用户登录的问题
- 求一编译课程设计
- 老子:无为而治