Cancel click event an a page

P

Paolo Mancini

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
 
M

Michael Winter

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?

In some browsers, yes.
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.

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

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 "application 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.
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.

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.
Any suggestion?

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
 
J

Jim Ley

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top