In its recent version, Edge shows a context menu when selecting text. Is there any way to prevent this behaviour with JavaScript? I tried
window.oncontextmenu = e => {e.preventDefault();};
but this wasn't successful.
In its recent version, Edge shows a context menu when selecting text. Is there any way to prevent this behaviour with JavaScript? I tried
window.oncontextmenu = e => {e.preventDefault();};
but this wasn't successful.
3 Answers
Reset to default 23If you don't need to use this feature in Edge, you can disable this menu via specific setting in Edge.
Just follow these steps:
- Naviagte to
edge://settings/appearance
in Edge. - Scroll down the page to the
Context menus
section. - Disable the option
Show mini menu when selecting text
.
Something like this:
In addition, you can also disable this feature on specific sites, depending on your needs.
window.onmouseup = event => event.preventDefault();
Following Dmitry Korenko tip, I made a version that adds the event only if the browser is Edge.
The problem with this event is that to cancel a selection you need to click outside the selected elements. This creates a problem because if you select everything with CTRL + A you cannot remove the selection as there is no unselected element to click on. That's why I made the second event that cancels the selection when double-clicking.
function disableEdgeMiniMenu() {
if (/Edg\//.test(window.navigator.userAgent)) { // if it's edge (chromium-based)
window.addEventListener("mouseup", event => event.preventDefault())
window.addEventListener("dblclick", () => window.getSelection().empty())
}
}
user-select: none
orpointer-events: none
on the wanted tag but it can be blocking for behaviors like text selection. Context menu is unfortunately only dedicated to the right-click context menu. – PedroZorus Commented Mar 28, 2022 at 15:29