[Webinar] Streamline your web hosting managementRegister Today


Prevent onmousedown event from bubbling up in IE

Posted on 2000-01-14
Medium Priority
Last Modified: 2012-06-21
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>
    document.anchors [0].onmousedown = function () {
        event.cancelBubble = true; // This doesn't seem to work.

Question by:avernet

Accepted Solution

garrethg earned 400 total points
ID: 2358811
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>
    document.anchors[0].onclick = function () {return false};


Author Comment

ID: 2361393
Cool and there is a oncontextmenu to handle the right mouse click. Thank you very much for your quick answer.


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question