As promised, here it is. Refer to the earlier articles if you want to know the hows and whys.
/*
* Created on 5/11/4
*
* Copyright 2004 Paul Marrington
* All rights reserved - http://marringtons.com
* PROPRIETARY/CONFIDENTIAL
* Use is subject to license terms - paul@marrington.net
*/
/**
* Add all the events in an event object
* inside the group given.
*/
Events.addEvents = function( element, group)
{
for (var name in group.events)
Events.add(
element, name, group.events[name]);
}
Events.addSameEvent = function(
name, action, elements)
{
for (var i = 2; i < arguments.length; i++)
Events.add( arguments[i], name, action);
}
Events.add = function( element, name, action)
{
name = name.toLowerCase();
var on = "on" + name;
if (! element.events)
element.events = new Object();
var list = element.events[name];
if (! list)
{
list = element.events[name] = new Array();
if (element[on]) list.push( element[on]);
/*
* This is an interesting bit of obscure code.
* If there were any other way, I would not do
* it, but IE makes it essential. The event handler
* is inline so that it keeps a handle to the surrounding
* function/object. This way we can set event.owner
* to the same as the calling element when add
* function was called - even though the event
* handler is called asynchronously later,
* long after the add function is dead and buried.
* Nasty. It also means we are hanging on to an
* unknown amount of stuff. The W3C DOM uses
* evt.currentTarget, but this is the ONLY way
* we can get IE to know the owner. This is
* because evt.target is the actual element
* clicked on or whatever, while the owner
* of the event can be further up the DOM tree.
*/
element[on] = function( evt)
{
/*
* Normalise the event object between
& browsers and retrieve the list
* of actions to take.
*/
evt = evt || event;
if (! evt.target)
evt.target = evt.srcElement;
/*
* We get element from the owner object.
*/
evt.owner = element;
var list = this.events[evt.type];
/*
* Run each event in the list until
* one returns false. This breaks the chain.
* Events are run from the most
* recently added to the oldest.
*/
for (var i = list.length - 1; i >= 0; i--)
{
evt.action = list[i];
if (! evt.action( evt))
return false;
}
return true;
}
}
list.push( action);
}
Events.newList = function( name)
{
var newList = function( evt)
{
for (var j = evt.action.events.length - 1;
j >= 0; j--)
if (! evt.action.events[j]( evt))
return false;
return true;
}
newList.eventName = name;
newList.events = new Array();
newList.add = function( event)
{
this.events.push( event);
}
return newList;
}
8 Comments:
Its pleasure to visit your blog and i am really in love with it.We have something regarding hosting too.We have our own iTalks Community.
iTalks Community hopes to become a great place for Web Designers, Web Masters, Graphics designers and Freelancers Hosting companies etc. to meet, discuss, and learn new techniques! We are planning to have a large database of tutorials to cover all the topics. We hope to encourage a free and open marketplace for trading upon the internet of designs, web hosting, and general internet solutions.
http://www.italks.net
We hope that you will enjoy your stay here and make this forum an active and happening place.
Regards,
vijay srivastava.
wow. my dad's name is paul marrington. ha.
-a visitor.
I've yet to actually use this code, but if it even does half of what you describe -- thank you. This is an issue I've been wrestling with for a while but have yet to come up with an elegant solution.
Again, thank you. Keep up the good work.
Hi there. Nice blog you have and I think this blog deserves better coverage.
I would like to ask for a link exchange between this blog of yours and mine.
If you accept my request, kindly add my link to your blog, proceed to my blog and tell me about it.
Ignore this if you decided to deny my request.
Hi there. Nice blog you have and I think this blog deserves better coverage and link exchange will lead you to it.
I would like to ask for a link exchange between this blog of yours and mine.
If you accept my request, kindly add my link to your blog, proceed to my blog and tell me about it.
Ignore this if you decided to deny my request.
hi,
its very nice blog.
& good place to get info about web development, webdesign, java scripts.
Great Tips on Software Development:
Software Development
Good blog related java script and software development.
Post a Comment
<< Home