I grabbed this "Rock Solid addEvent" code from this site, which is based on Mark Wubben's event-cache code. (These links for reference only.)
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: -
addEvent(nodeid, 'click', myFunc);
-
Second question: Why can I not pass a value in the function using this code? Example, I get an error with this: -
addEvent(window, 'load', myFunc(value));
-
And without further ado, here be the code (a long and nasty bugger): -
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;
-
};
-
}
-
};
-
}();
-
Oh, and one last question...why the () at the very end of this code?
1 1827
The whole problem is about mixing 2 different things. You have to make clear when you are working with references to objects/functions and when you are working just with their results. As we may know: - myFunc // is just reference to function with "myFunc" name
-
myFunc() // is result of executing myFunc function
While keeping this in mind, we can answer most of your questions. addEvent(obj, type, fn) function is expecting 3 attributes
obj - REFERENCE to the DOM object
type - string representation of event name (without 'on')
fn - REFERENCE to your event handler function
So, first answer: - addEvent(nodeid, 'click', myFunc);
use - addEvent(document.getElementById('nodeid'), 'click', myFunc);
Second answer: - addEvent(window, 'load', myFunc(value));
In this calling, JS will execute myFunc function first and send it's return value as 3th parameter to addEvent function. So while your myFunc function is not returning reference to any other function, this code is invalid.
Third answer:
"why the () at the very end of this code"
Simple, creator dont want to assign function reference to EventCache. He wants to assign result of function execution to EventCache. Think of it that way: - var EventCache;
-
-
anonymousfunction = function()
-
{
-
... code here
-
-
return something;
-
}
-
-
EventCache = anonymousfunction();
-
so in the end EventCache is holding RESULT of running anonymousfunction.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ben |
last post by:
Hello everybody
I got confused by this problem for which I don't have a logical explanation.
There is a Thread (ThreadA) which receives Events from another system thread
(ThreadS). ThreadA then...
|
by: Jayman777 |
last post by:
Hello, I am using PHP 4.4.1 and am having problems retrieving an object
from an array of objects and then calling a method on it. Here are two
simple classes that I am using:
class Day...
|
by: Bob |
last post by:
Hi,
Hope you can help me with this one. I'm at my wits end. I'm trying to
create an intelligent edit-box like the
excellent "Customer" one at the URL:
...
|
by: joaotsetsemoita |
last post by:
Hello everyone,
I'm having troubles assigning an onclick event to a cell. Im trying
something like
cursorPoint.cells.style.cursor = "hand";
cursorPoint.cells.width = "20";...
|
by: RMWChaos |
last post by:
Darnit all, I expect the code I steal from others to work! =D
Below is some code that I got to initiate multiple javascripts on page load (rather than using the "onload=" attribute). According the...
|
by: tader |
last post by:
Hi, so i done script like that
body : function() {
for (i = 0; i < all_divs.length; i++) {
if (all_divs.title) {
addEvent(all_divs, "mouseover", this.onover);...
|
by: Aaron Gray |
last post by:
I jokingly say this is the late entry :)
Okay I have read all the event entry comments from John's Resig's AddEvent
comepition blog :-
http://ejohn.org/projects/flexible-javascript-events/
...
|
by: Dormilich |
last post by:
Organize your Scripts
what you should have:
basic Javascript understanding
a standard compliant browser
additional goodies:
Firefox with the Firebug plugin installed
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |