...

View Full Version : Canceling default actions?



RIK
01-08-2004, 04:59 PM
Hiya,
I'm trying to determine of it's possible to cancel default actions of event handlers (specifically onClick handlers) page-wide. I'm trying to do this to save myself massive recoding of a bunch of links and form controls over a bunch of pages I've inherited from another developer.

I can detect the event and identify the srcElement, then call a function just fine (document.onclick = checkEl), and the function fires, but I can't cancel the srcElement's default action.

I've tried to do this by using event.returnValue="false" like so:

document.onclick=checkEl;

function checkEl() {
if(event.srcElement.id=='someId'){
window.event.returnValue= "false"
}
}

(Later in the script I'll be checking a variable value to determine whether to allow the origional event handler action to continue or initiate a different function call - but that's not the trouble spot so i left that bit out)


When i've toyed around with this, it works fine if the function which sets the returnValue is set to false in the default action (I.e., in the function called by the srcElement's onClick handler) so I'm pretty sure I have the syntax correct, but if I try to cancel the default action at the window level, the default action still occurs, THEN the function called by window.onclick fires.

Any suggestions as to how to get this working, or is it not possible?

Oh - working in IE 6.0...

Thanks in advance for any help.

Cheers,
RIK

Mr J
01-09-2004, 12:19 AM
Please try the following


function checkEl() {
if(event.srcElement.id=='someId'){
window.event.returnValue=false
}
}

Choopernickel
01-09-2004, 02:32 PM
Here's a handy little function I made that safely and effectively cancels an event in any browser that supports event cancellation, I believe:

function cancelEvent (e) {
if (e) {
try {
e.returnValue = false;
} catch (er) {} // eat the exception
try {
e.cancelBubble = true;
} catch (er) {} // eat the exception
try {
e.preventDefault();
} catch (er) {} // eat the exception
try {
e.stopPropagation();
} catch (er) {} // eat the exception
}
return false;
}

Use it instead of return false; - as in return cancelEvent();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum