Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    RIK
    RIK is offline
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Canceling default actions?

    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

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Please try the following


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

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's a handy little function I made that safely and effectively cancels an event in any browser that supports event cancellation, I believe:
    Code:
    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();


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •