On Jan 1, 5:30 am, Ken Snyder <[EMAIL PROTECTED]> wrote:
> RFLudwick wrote:
> > I'm trying to tie in all of a website's event handlers in HTML to
> > Prototype. I.e.
Why? What are you trying to achieve? What is it in Prototype.js'
handler scheme that you want to use?
> > <a onclick="..."></a>
>
> > I can't easily strip out these event handlers from the HTML and put
> > them in Javascript, but I would like to get them into Prototype's
> > event handling system so they can unregister for IE, etc.
It's pretty simple to "unregister" an inline handler, simply set the
attribute value to null, e.g.:
element.onclick = null;
> > I believe what I'm looking for is Event.extend(), but the
> > documentation on it is minimal and it tells of extending an event
> > registered in other ways. But how exactly do I retrieve the onclick
> > event of a link as it is defined in HTML?
>
> > ...
>
> Doesn't sound like a good idea.
Not at all.
> Such code will be pretty complicated and
> run slowly. Here is something that conceptually would work:
It doesn't. It can be made to work on a few browsers with significant
changes, but IE refuses to let it "work" at all.
> var code, fn, elements = document.getElementsByTagName('*');
> // check each element
> for (var i = 0, len = elements.length; i < len; i++) {
> // check each possible event (whatever events you might find in your code)
> $w('blur focus resize scroll load unload click dblclick mousedown
> mouseup mousemove mouseover mouseout change reset select submit keydown
> keypress keyup error').each(function(eventName) {
> if (code = elements[i]['on' + eventName]) {
> // replace "return false" with "Event.stop()" in function body
> code = String(code).replace('return false', 'Event.stop(event)');
That does not seem appropriate. Event.stop() is not equivalent to
handler returning false.
> // create a function by evaluating the contents of the event attribute
> fn = eval('function(event) { ' + code + '}');
The function code is already wrapped in 'function (event){'...'}' in
most browsers. In IE it is already wrapped in 'function anonymous()
{'...'}'. Further wrapping seems pointless, unless you intend to
strip off the first wrapping before adding the second.
At this point IE errors and processing stops:
Error: fn is null or not an object
> // translate event names as needed
> eventName = (eventName == 'load' : 'dom:loaded' : eventName);
I think you meant:
eventName = (eventName == 'load' ? 'dom:loaded' : eventName);
which seems superfluous as the function should be called when the load
(or dom:load) event fires, so it has already occurred. Changing it
now is pointless unless there is an intention to call it manually
later, in which case it probably shouldn't be an onload (or dom:load)
handler.
[...]
> There may be other things to account for.
Yes. A function that does seem to work is:
function changeHandlers() {
var el, els = document.getElementsByTagName('*');
var fn, evt, onEvt;
// List the handlers to be changed
var evtArray = ('click blur').split(' ');
for (var i=0, leni=els.length; i<leni; i++) {
el = els[i];
for (var j=0, lenj=evtArray.length; j<lenj; j++) {
evt = evtArray[j];
onEvt = 'on' + evt;
if (el[onEvt]) {
fn = el[onEvt];
// Remove the original handler
el[onEvt] = null;
// Re-attach it
$(el).observe(evt, fn.bindAsEventListener(el));
}
}
}
// Make sure circular references are avoided.
el = els = null;
}
If the OP really wants to mess with the function body code, and that
seems a particularly bad thing to to, something like the following may
suit:
fn = String(el[onEvt]).replace(/^[^{]+\{/,'').replace(/\}[^}]*
$/,'');
fn = new Function(fn + ';extra code here;');
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---