I am having two problems with it, and the webmaster is s...l...o...w to respond. So I thought I would ask about it here. If I knew more about how this code works, I could probably figure out most of the problems myself, but with this one, I am clueless! =D
First question: This may be my own unfamiliarity with attachEvent(), but I cannot seem to use addEvent() for onclick, onmouseover, and onmouseout. I tried just using attachEvent() directly, but got an error message that "object type does not support this method". Well, [insert favorite expletive here]!! So this code does not work for me:
Expand|Select|Wrap|Line Numbers
- addEvent(nodeid, 'click', myFunc);
Expand|Select|Wrap|Line Numbers
- addEvent(window, 'load', myFunc(value));
Expand|Select|Wrap|Line Numbers
- function addEvent(obj, type, fn)
- {
- if (obj.addEventListener) // FireFox, NN, Mozilla, etc.
- {
- obj.addEventListener(type, fn, false);
- // bugzilla bug #241518
- EventCache.add(obj, type, fn);
- }
- else if (obj.attachEvent) // MSIE
- {
- var func = function()
- {
- fn.apply(window.event.srcElement);
- };
- obj.attachEvent("on" + type, func);
- EventCache.add(obj, type, func);
- }
- else if (typeof obj['on' + type] != 'function')
- {
- obj['on' + type] = fn;
- }
- else // really old
- {
- var oldonload = obj['on' + type];
- obj['on' + type] = function()
- {
- oldonload();
- fn();
- };
- };
- };
- function removeEvent(obj, type, fn)
- {
- EventCache.remove(obj, type, fn);
- };
- var EventCache = function()
- {
- var listEvents = [];
- return {
- listEvents : listEvents,
- add : function(node, sEventName, fHandler)
- {
- listEvents.push(arguments);
- },
- remove : function(node, sEventName, fHandler)
- {
- var i, item;
- for(i = listEvents.length - 1; i >= 0; i = i - 1)
- {
- if(node == listEvents[i][0] && sEventName == listEvents[i][1] && fHandler == listEvents[i][2])
- {
- item = listEvents[i];
- if(item[0].removeEventListener)
- {
- item[0].removeEventListener(item[1], item[2], item[3]);
- };
- if(item[1].substring(0, 2) != "on")
- {
- item[1] = "on" + item[1];
- };
- if(item[0].detachEvent)
- {
- item[0].detachEvent(item[1], item[0][sEventName+fHandler]);
- };
- item[0][item[1]] = null;
- };
- };
- },
- flush : function()
- {
- var i, item;
- for(i = listEvents.length - 1; i >= 0; i = i - 1)
- {
- item = listEvents[i];
- if(item[0].removeEventListener)
- {
- item[0].removeEventListener(item[1], item[2], item[3]);
- };
- if(item[1].substring(0, 2) != "on")
- {
- item[1] = "on" + item[1];
- };
- if(item[0].detachEvent)
- {
- item[0].detachEvent(item[1], item[2]);
- };
- item[0][item[1]] = null;
- };
- }
- };
- }();