Disable Right Click Using MooTools 1.2

By  on  

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?

Recent Features

  • By
    39 Shirts – Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

Incredible Demos

  • By
    Disable Autocomplete, Autocapitalize, and Autocorrect

    Mobile and desktop browser vendors do their best to help us not look like idiots by providing us autocomplete, autocorrect, and autocapitalize features.  Unfortunately these features can sometimes get in the way;  we don't always want or need the help they provide.  Luckily most browsers allow...

  • By
    Record Text Selections Using MooTools or jQuery AJAX

    One technique I'm seeing more and more these days (CNNSI.com, for example) is AJAX recording of selected text. It makes sense -- if you detect users selecting the terms over and over again, you can probably assume your visitors are searching that term on Google...

Discussion

  1. Oliver

    contextmenu eh?? hmmm, news to me.

  2. 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…

  3. awake

    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…

  4. I think e.stop() is enough, because it already do preventDefault in the Mootools Core source.

  5. @Lim Chee Aun: Good call! I’ve adjusted the code. Thank you for mentioning that.

  6. 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.

  7. @Ahmed: Thanks — I have been working on a custom context menu…

  8. @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?

  9. 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.

  10. Quit good for prevent picture steeling ;) ….

  11. great stuff, it’s new to me to heard about contextmenu, but anyway good job…. hope you will work more about mootools….

  12. @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.

  13. Why document.body and not document.documentElement (i.e. html tag)? What id body has a margin, or html has a padding set?

  14. I don’t this will prevent displying context menu from opera…as far as I know you can’t disable context menu in opera….

  15. @Roshan: That’s correct.

  16. imran

    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

  17. David,

    For compatibility with IE7, shouldn’t that be $(document.body) rather than just document.body?

    Rob

  18. 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 to document.body, so

    document.body.oncontextmenu="";
  19. refreegrata

    sorry, I refer to the “a” elements, but in the comments was taken like code.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!