Prev: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2010-01-29)
Next: Howto: Open a Javascript popup from an external file
From: john_woo on 29 Jan 2010 11:23 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 29 Jan 2010 12:31 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 29 Jan 2010 13:07 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 2 Feb 2010 04:09 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 2 Feb 2010 04:52
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. |