Create your own right-click contextmenu

Finley

New Member
Sep 23, 2011
11
1
Hi there,

I am going to explain to you how to make your custom right-click contextmenu. I am going to explain every part.
It is very simple. We will be using jQuery so include the library in your <head> tag.

I am asuming you have the [very, very] basic knowledge of HTML, CSS and JavaScript
Step 1
We begin with creating an simple eventlistener.
Code:
window.addEventListener('contextmenu', function(e) {
    // When you click on the contextmenu, trigger this function
})

Step 2
In this step we will be creating the 'pop-up' of our own context menu. We are using the jQuery .css() function for that.
Code:
    $('.rightclickmenu').css({ // $('CHANGE_THIS_TO_YOUR_MENU_CLASS')
        "margin-left": e.clientX, // position of the mouse X
        "margin-top": e.clientY // position of the mouse Y
    }).show() // Showing the actual menu

Step 3
In the 3rd step we are going to disable the default context menu, so it doesn't show up when you right-click your screen.
For this, is one simple line needed.
Code:
e.preventDefault();

Step 4
In the 4th and final step we are going to make the custom menu dissapear, as soon as you click somewere out of the menu.
We will be using another eventlistener for that
Code:
window.addEventListener('click', function(){ // as soon as you click
     $('.rightclickmenu').hide(); // hide the contextmenu
})

Okay then, lets put it all together now

It will look something like this:
Code:
window.addEventListener('contextmenu', function(e) {
    $('.rightclickmenu').css({
        "margin-left": e.clientX,
        "margin-top": e.clientY
    }).show()

    e.preventDefault();
    window.addEventListener('click', function(){
        $('.rightclickmenu').hide();
    })
})

For a full HTML page:
For a live demo:

Kind regards

Finley
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Nice. These sorts of things are good for web apps, i.e: music players to have play, pause, next, previous etc controls in the menu or WYSIWYG editors for controls such as cut, copy, paste (although they come as default), undo, redo, bold, underline etc.

The only thing you have done wrong in my opinion is assigning the id 'rightclickobject' multiple times. Remember ID's must be unique.
 

Finley

New Member
Sep 23, 2011
11
1
Nice. These sorts of things are good for web apps, i.e: music players to have play, pause, next, previous etc controls in the menu or WYSIWYG editors for controls such as cut, copy, paste (although they come as default), undo, redo, bold, underline etc.

The only thing you have done wrong in my opinion is assigning the id 'rightclickobject' multiple times. Remember ID's must be unique.

Yeah, my css is kinda meh. But I was too lazy to give them an individual name.
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Nice tutorial. This'll come in handy for quite a few people like Mark said.

Only issue with using this is, it must be used properly. By default (unless instructed to) this menu is out of view, therefor out of the users mind. So, I'd only really use this on a page where right clicking is either instructed or obvious to do so.
 

Finley

New Member
Sep 23, 2011
11
1
Nice tutorial. This'll come in handy for quite a few people like Mark said.

Only issue with using this is, it must be used properly. By default (unless instructed to) this menu is out of view, therefor out of the users mind. So, I'd only really use this on a page where right clicking is either instructed or obvious to do so.
You could also use it to prevent users to inspect your source that easy. I've seen it on a retro hotel.

Verstuurd vanaf mijn HTC Sensation XE with Beats Audio Z715e met Tapatalk
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
You could also use it to prevent users to inspect your source that easy. I've seen it on a retro hotel.
Well, it stops the obvious way but you can still f12 and Ctrl+u ;p
Verstuurd vanaf mijn HTC Sensation XE with Beats Audio Z715e met Tapatalk
 

GarettM

Posting Freak
Aug 5, 2010
833
136
You could also use it to prevent users to inspect your source that easy. I've seen it on a retro hotel.

Verstuurd vanaf mijn HTC Sensation XE with Beats Audio Z715e met Tapatalk
That doesn't work and to be honest its just stupid. Your source code can be seen with ctrl + u(chromium) or through the browser menu.
Back on topic: This is useful thanks! And I am not sure but I believe it's good practice to include JavaScript files at the end of the body tag to reduce load time.
 

Finley

New Member
Sep 23, 2011
11
1
That doesn't work and to be honest its just stupid. Your source code can be seen with ctrl + u(chromium) or through the browser menu.
Back on topic: This is useful thanks! And I am not sure but I believe it's good practice to include JavaScript files at the end of the body tag to reduce load time.

Yeah iknow but sometimes people forget to use ctrl+u and are only using inspect element. Bit yeah, this was ment as a little goodie.

Verstuurd vanaf mijn HTC Sensation XE with Beats Audio Z715e met Tapatalk
 

Users who are viewing this thread

Top