Prevent onmousedown event from bubbling up in IE

Ok, I think this question is one of the FAQ. But here it is anyway :-). I would like to catch the onmousedown and prevent the browser to interpret it. (I am doing this to display a layer, simulating a contextual menu, where the right mouse button is clicked.)

I have some code that works fine in Netscape, but I can't reproduce the same result with IE. When the event is fired, I set the event.cancelBubble to true, but it doesn't seem to have any effet.

So for instance, how to prevent the browser to go to Yahoo! (and just do nothing instead) when the "Yahoo!" link is clicked in the example below:

<a href="http://www.yahoo.com/">Yahoo!</a>
<script>
    document.anchors [0].onmousedown = function () {
        event.cancelBubble = true; // This doesn't seem to work.
    };
</script>


Alex
LVL 1
avernetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

garrethgCommented:
As I understand it, cancelBubble merely prevents the event 'bubbling' UP to the next object in the document which could receive the event. So it isn't what you're looking for to cancel the event here.

You need to return false to cancel the event, however mousedown or mouseup are not the triggering events for a link; onclick is.

Try this (note: the anchor needs an ID or NAME):

<a ID="anchor1" href="http://www.yahoo.com/">Yahoo!</a>
<script>
    document.anchors[0].onclick = function () {return false};
</script>

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
avernetAuthor Commented:
Cool and there is a oncontextmenu to handle the right mouse click. Thank you very much for your quick answer.

Alex
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.