Cancel click event an a page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paolo Mancini

    Cancel click event an a page

    Hi all,

    I have a page with many different elements: <a href="...."> ... </a>,
    listboxes, radiobuttons, etc.

    Can I use javascript to prevent the user from interacting with the
    page? That is: can I intercept, alert the user, and cancel any click
    event on the page?

    Unfortunately, if the user clicks on a page element, the event bubbles
    up from the element up in the hierarchy to the document, not
    viceversa. I would like to intercept click events at document element
    BEFORE they fires on the objects.

    I know for example that onclick="return false" cancels events for <a>
    elements, but still the user is able to interact with <select>
    objects.

    I have tried to place a large, transparent <DIV> with z-index=1000,
    capture the onclick event and cancel it, but it doesn't work. Any
    suggestion?

    Regards,

    Mauro
  • Michael Winter

    #2
    Re: Cancel click event an a page

    On 19 Apr 2004 09:36:50 -0700, Paolo Mancini <paomanci@yahoo .it> wrote:
    [color=blue]
    > I have a page with many different elements: <a href="...."> ... </a>,
    > listboxes, radiobuttons, etc.
    >
    > Can I use javascript to prevent the user from interacting with the
    > page? That is: can I intercept, alert the user, and cancel any click
    > event on the page?[/color]

    In some browsers, yes.
    [color=blue]
    > Unfortunately, if the user clicks on a page element, the event bubbles
    > up from the element up in the hierarchy to the document, not
    > viceversa. I would like to intercept click events at document element
    > BEFORE they fires on the objects.[/color]

    Actually, events move from the document root to the event target, then
    back up to the document root. This downward movement (propagation) is
    called the event capture phase, the others being the "at target" and
    "bubbling" phases, but IE knows nothing of this concept. I find this quite
    surprising as even NN4 supports it in a rudimentary fashion.
    [color=blue]
    > I know for example that onclick="return false" cancels events for <a>
    > elements, but still the user is able to interact with <select>
    > objects.[/color]

    Though the click event is always cancellable, browsers don't have to
    honour it in every case. As Jim Ley pointed out to me recently, some
    actions may be considered "applicatio n level", and so are outside the
    scope of scripts. Displaying the drop-down list with a SELECT element will
    undoubtably be one such case. Don't forget that some events can never be
    cancelled, no matter what their target.
    [color=blue]
    > I have tried to place a large, transparent <DIV> with z-index=1000,
    > capture the onclick event and cancel it, but it doesn't work.[/color]

    I wouldn't expect it to. Events should be directed to the deepest node
    (not necessarily a leaf) in the document tree. Z-index shouldn't be a
    factor in anything but presentation.
    [color=blue]
    > Any suggestion?[/color]

    A general strategy would be laborious, and unreliable in some cases. If
    built-in event capturing is unsupported, it would involve walking through
    the entire document (therefore requiring a recent DOM-compliant browser)
    and adding event listeners to all elements that you don't want to receive
    events. You can stop propagation, so it won't be every element in the
    document. Even then, some actions may still occur.

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Jim Ley

      #3
      Re: Cancel click event an a page

      On Mon, 19 Apr 2004 17:05:23 GMT, Michael Winter
      <M.Winter@bluey onder.co.invali d> wrote:
      [color=blue]
      >A general strategy would be laborious, and unreliable in some cases. If
      >built-in event capturing is unsupported, it would involve walking through
      >the entire document (therefore requiring a recent DOM-compliant browser)
      >and adding event listeners to all elements that you don't want to receive
      >events. You can stop propagation, so it won't be every element in the
      >document. Even then, some actions may still occur.[/color]

      IE's propriatary setCapture method can do this, and is a very useful
      method, but nowhere near foolproof right click, left click, right
      click almost always removes the capturing much to the amusement of
      people asking me how to close a window after it all gone wrong.

      there's not going to be a simple perfect solution, and if it's just to
      suggest that a user not activate those links then it can be a useful
      enhancement.

      Jim.

      --
      comp.lang.javas cript FAQ - http://jibbering.com/faq/

      Comment

      Working...