I am creating a Chrome App, but I would like to do something like right-click the menu like Chrome Dev Editor.
What kind of event handling should I do to detect a right click?
For your information only
I'm not familiar with Chrome app, so I don't know about it, but
If it's normal js, would it be like the following?
// Let's say the id is something.
variable= document.getElementById("something");
element.onclick=function(e){
if(e.button===2){
// Write down the right-click process in here.
}
}
Reference
click-Event reference|MDN
Another solution I found while researching myself was that I was able to implement it by processing the contextmenu event.
varem= document.querySelector('#targetElement');
elem.addEventListener('contextmenu', function(e){
// block browser default right-click behavior
e.preventDefault();
// Displaying the DOM element representing the UI of the context menu in the mouse position
varcontextMenu= document.querySelector('#myContextMenu');
contextMenu.classList.remove('hidden');
contextMenu.style.left=e.x+'px';
contextMenu.style.top=e.y+'px';
});
© 2024 OneMinuteCode. All rights reserved.