views:

1117

answers:

2

I'm trying to expand navigation options of the context menu on certain elements (specifically, h1 and h2 tags) I want to prevent the browser's default action when right-clicking on those elements.

I found nice information at this page.

However, I couldn't find how to disable the context menu for certain elements. Does someone know how to do it?

I'm using prototype as my javascript API.

+1  A: 

This will prevent the context menu from appearing on a particular element

$(it).observe("contextmenu", function(e){
    e.stop();
});

So, for example stop all H1/H2 tags from showing a context menu

$$('h1, h2').each(function(it){
    $(it).observe("contextmenu", function(e){
        e.stop();
    });
})
kouPhax
A: 

You can obfuscate it a bit, but ultimately your page is only a guest in the browser in, (and you can take that to mean in the same manner that a prisoner is a "guest" of the state, if you wish). Therefore the page must rely on the browser to play nice. If the user wants to run a browser that doesn't play nice, or customize their existing browser to do so, that is always their option. You can never force a browser to do anything. Nothing you can do will be able to stop the user from performing a given activity with their browser if they really want to, once they view a page on their local machine. More than that, most recent browsers have facilities already built in to make it very easy for the user to override the normal behavior when something seems out of the ordinary.

Joel Coehoorn
I don't know how this relates to the original question?
kouPhax
He want's do disable a context menu in the browser. Down that road lies madness.
Joel Coehoorn
That may be so but it's just another event in Javascript, I don't see that it raises any extra issues above and beyond other more common events.
kouPhax
The problem is that while your answer above may _seem_ to do what he wants, the truth is that he really _can't_ disable the menu for all users. Anyone who really wants to see that menu still can. In practice, it's trivial to get around that code and view the menu anyway.
Joel Coehoorn
True but the same can be said for 99.9% of Javascript based interactions. This is no different. Web/Javascript development should always consider edge cases but you shouldn't just not do it becasue there is potential loopholes. That's defeatist :-P
kouPhax
I'm more concerned they're using this to "secure" some kind of content, in which case they'll find that they've failed utterly.
Joel Coehoorn