Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger right click using pure Javascript

Tags:

javascript

How can I manually trigger a right click using Javascript?

I can do this with jQuery but I can only use pure Javascript in this case.

like image 779
Abs Avatar asked Oct 27 '11 10:10

Abs


People also ask

How do you right-click in JavaScript?

To specifically capture the mouse right-click, JavaScript has a special event for that - contextmenu . Depending on the situation, instead of identifying which button has been clicked, you can specify the contextmenu event. This event still has the button property, with the number value of the, clicked button.

How do I make a custom right-click?

To make this HTML menu visible when right-clicking in the browser, we need to listen for contextmenu events. We can bind the event listener directly on the document or we can limit the area that a user can right-click to see the custom context menu. In our example, the area for right-click is the entire body element.

How do I trigger an event in Contextmenu?

The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse button, or by pressing the context menu key.

How do you trigger in JavaScript?

onchange: It is triggered when an HTML element changes. onclick: It is triggered when an HTML element is clicked. onmouseover: It is triggered when the mouse is moved over a HTML element. onmouseout: It is triggered when the mouse is moved out of a HTML element.


2 Answers

Create an event using the CustomEvent constructor, or (when it is not supported), create an event using document.createEvent with as argument the string "HTMLEvents" (because you're going to create a click event). Then, use the initEvent method to create a click event.

Finally, use the dispatchEvent method to fire the event. If you're using IE, the fireEvent method has to be used instead.

If you want to trigger the rightclick event, contextmenu, use the code below:

var element = document.getElementById('yourElement'); if (window.CustomEvent) {     element.dispatchEvent(new CustomEvent('contextmenu')); } else if (document.createEvent) {     var ev = document.createEvent('HTMLEvents');     ev.initEvent('contextmenu', true, false);     element.dispatchEvent(ev); } else { // Internet Explorer     element.fireEvent('oncontextmenu'); } 
like image 161
Rob W Avatar answered Oct 05 '22 09:10

Rob W


Another variant, this time using the MouseEvent API which is a bit more modern. In my case I actually send all three events mouseup/mousedown/contextmenu:

var element = document.getElementById("yourElement"); var ev1 = new MouseEvent("mousedown", {     bubbles: true,     cancelable: false,     view: window,     button: 2,     buttons: 2,     clientX: element.getBoundingClientRect().x,     clientY: element.getBoundingClientRect().y }); element.dispatchEvent(ev1); var ev2 = new MouseEvent("mouseup", {     bubbles: true,     cancelable: false,     view: window,     button: 2,     buttons: 0,     clientX: element.getBoundingClientRect().x,     clientY: element.getBoundingClientRect().y }); element.dispatchEvent(ev2); var ev3 = new MouseEvent("contextmenu", {     bubbles: true,     cancelable: false,     view: window,     button: 2,     buttons: 0,     clientX: element.getBoundingClientRect().x,     clientY: element.getBoundingClientRect().y }); element.dispatchEvent(ev3); 
like image 41
jmiserez Avatar answered Oct 05 '22 08:10

jmiserez