From: john_woo on
Hi,

I need to implement API by which all onclick envets in same document
can be in flexible control (disable/enable on demand).

I tried the followings:

1. overwrite onclick function:
if (window.captureEvents){
window.captureEvents(Event.CLICK);
window.onclick=myfunction;
}
else{
document.captureEvents(Event.CLICK);
document.onclick=myfunction;
}

document.addEventListener("click", myfunction, true); //or
document.attachEvent("click", myfunction);

2. then in myfunction:
if (event.stopPropagation) event.stopPropagation();
else event.cancelBubble = true; //or
if (event.preventDefault) event.preventDefault();
else event.returnValue = false;

However, the above can only overwrite default behavior, can't be
applied to those buttons which are associated with specific functions/
handlers;

Then traversing through document tree to find out and overwrite those
functions. This leads to big problem - when enabling onclick events,
those specific handling functions gone!

I'm wondering, what is the right way to design/implement such API?

Thanks,
--
John
From: Thomas 'PointedEars' Lahn on
john_woo wrote:

> I need to implement API by which all onclick envets in same document
> can be in flexible control (disable/enable on demand).
>
> I tried the followings:
>
> 1. overwrite onclick function:
> if (window.captureEvents){
> window.captureEvents(Event.CLICK);
> window.onclick=myfunction;
> }
> else{
> document.captureEvents(Event.CLICK);
> document.onclick=myfunction;
> }

I wonder how old the tutorial has been where you read that nonsense.

> document.addEventListener("click", myfunction, true); //or
> document.attachEvent("click", myfunction);

The two calls are _not_ remotely equivalent (who told you to pass `true'
for the third argument anyway?):

<http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html>

> 2. then in myfunction:
> if (event.stopPropagation) event.stopPropagation();
> else event.cancelBubble = true; //or
> if (event.preventDefault) event.preventDefault();
> else event.returnValue = false;

It is one thing to prevent the default action for an event,
another to stop its propagation in the document tree.

> However, the above can only overwrite default behavior,

No, preventDefault() or `returnValue' can prevent the default action, and
stopPropagation() or `cancelBubble' prevent the propagation of the event in
the document tree. However, stopPropagation() is _not_ equivalent to
`cancelBubble'; the latter can only prevent upwards propagation (event
bubbling).

> can't be applied to those buttons which are associated with specific
> functions/handlers;

Event listeners are associated with (added to) elements, not vice-versa.

> Then traversing through document tree to find out and overwrite those
> functions. This leads to big problem - when enabling onclick events,
> those specific handling functions gone!

You cannot overwrite event listeners. You can remove them, but you
need a reference to them first, and AFAIK it cannot be retrieved without
a user-defined event registry.

> I'm wondering, what is the right way to design/implement such API?

Do not do this. What do you think you need it for anyway?


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: Jorge on
On Jan 29, 5:23 pm, john_woo <john_...(a)canada.com> wrote:
> (...)
> I'm wondering, what is the right way to design/implement such API?
>

There's no way... that's why people overlay a div on top of the page:
to mimic a modal dialog (and to capture -and stop the propagation- of
clicks).
--
Jorge.
From: wilq on
On Jan 29, 7:07 pm, Jorge <jo...(a)jorgechamorro.com> wrote:
> On Jan 29, 5:23 pm, john_woo <john_...(a)canada.com> wrote:
>
> > (...)
> > I'm wondering, what is the right way to design/implement such API?
>
> There's no way... that's why people overlay a div on top of the page:
> to mimic a modal dialog (and to capture -and stop the propagation- of
> clicks).
> --
> Jorge.

Adding an overlay should fix your problem as Jorge pointed out... Have
you tried that?
From: Asen Bozhilov on
wilq wrote:

> Adding an overlay should fix your problem as Jorge pointed out... Have
> you tried that?

How can you stop access keys with overlay? And i can't understand, why
OP want to do this?

function clickHandler()
{
if (someSpecialCase)
{
return;
}

//do something
};

He can check for special case in execution context created when
invokes handler function.