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.
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.
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.
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.
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.
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'); }
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With