Disable Right Click Using MooTools 1.2
We all know that it's bad practice to disable the right click context menu, so I'll skip the explanation of why that is. Unfortunatley, however, customer request comes before anything else when you're doing their website. If your project is using MooTools 1.2, you can achieve this goal.
The MooTools 1.2
window.addEvent('domready', function() {
document.body.addEvent('contextmenu',function(e) {
e.stop();
});
});
Again, the only time I would do this is if the customer asked for it. It's good to know that it's so easy to implement, right?
![CSS @supports]()
Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS. What we end up doing is repeating the same properties multiple times with each browser prefix. Yuck. Another thing we...
![Interview with a Pornhub Web Developer]()
Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...
![Scrolling “Agree to Terms” Component with MooTools ScrollSpy]()
Remember the good old days of Windows applications forcing you to scroll down to the bottom of the "terms and conditions" pane, theoretically in an effort ensure that you actually read them? You're saying "No David, don't do it." Too late -- I've done...
![Element Position Swapping Using MooTools 1.2]()
We all know that MooTools 1.2 can do some pretty awesome animations. What if we want to quickly make two element swap positions without a lot of fuss? Now you can by implementing a MooTools swap() method.
MooTools 1.2 Implementation
MooTools 1.2 Usage
To call the swap...
contextmenu eh?? hmmm, news to me.
ZOMG Bad practice alert!!!
Kidding, you can only argue that so far. Also, this is quite useful should you want to implement your own context menu…
dave…
whad do yo use to format source code on your blog post? (the code that puts the line #’s, copy to clipboard, e.t.c.)
thanks…
I think e.stop() is enough, because it already do preventDefault in the Mootools Core source.
@Lim Chee Aun: Good call! I’ve adjusted the code. Thank you for mentioning that.
This is also so useful when planning to add a custom context menu to your page, or a custom menu appears when you right click a certain element (a hyperlink, a button, or an image).
Thanks for sharing, David.
@Ahmed: Thanks — I have been working on a custom context menu…
@David: You’re welcome! Will you populate the custom context menu?
PS: Is it possible to work a clipboard manager (copy selection to clipboard) using MooTools?
uhg. I do not like this. Disabling my browsers features is not cool, I would be upset at any site or service that pulled this.
@Ahmed,
javascript cannot access the clipboard(which is a good thing) unless the browser has some feature that allows this(like older builds of IE). However, with the help of flash, you can do it, which is what syntaxhighlighter(in use here) does.
Quit good for prevent picture steeling ;) ….
great stuff, it’s new to me to heard about contextmenu, but anyway good job…. hope you will work more about mootools….
@weblizzer: To put it simple, a context menu is the menu that appear when you rightclick somewhere, like on Windows desktop for example, the “Refresh New-> etc..” is a contextmenu.
Why document.body and not document.documentElement (i.e. html tag)? What id body has a margin, or html has a padding set?
I don’t this will prevent displying context menu from opera…as far as I know you can’t disable context menu in opera….
@Roshan: That’s correct.
hi,
its all fine, but do you have any solution to show Custom Context Menu on right click in Opera???
if you find any solution please reply to my email , immidiately, you help will be highly appriciated, thanks
imran
This article could also assist: http://laymensterm.blogspot.com/2008/12/disable-copy-command-and-right-click.html
David,
For compatibility with IE7, shouldn’t that be $(document.body) rather than just document.body?
Rob
For reference, you should be able to disable this by overriding the
oncontextmenu
event in the javascript console. It looks like this is being added todocument.body
, soThanks. This post is very helpful to me. I have a question. I want to display a classic “lightbox” popup when the user do a click over an “” element and I want to disable the context menu in the “” elements to prevent the posibility of open a new window or new tab . But in Firefox and Opera I still can open a new tab with a click of the scroll button in the mouse. You do know some way to disable the default associated to a click with the scroll button?
Thanks, and sorry for my poor english.
sorry, I refer to the “a” elements, but in the comments was taken like code.