最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent context menu in Edge when text is selected - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question asked Mar 28, 2022 at 15:07 QwertyMQwertyM 1431 silver badge5 bronze badges 1
  • 1 You can use the css properties user-select: none or pointer-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
Add a comment  | 

3 Answers 3

Reset to default 23

If you don't need to use this feature in Edge, you can disable this menu via specific setting in Edge.

Just follow these steps:

  1. Naviagte to edge://settings/appearance in Edge.
  2. Scroll down the page to the Context menus section.
  3. 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())
    }
}
发布评论

评论列表(0)

  1. 暂无评论