Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to capture "Open in New Tab" clicked event of context menu using javascript?

I know I can use capture the Right Click event by using jQuery's "contextmenu" but my question is, how can I capture the event after the context menu appears i.e. When the user clicks on "Open Link in New Tab" action.

Any help?

Thanks.

enter image description here

like image 662
pso Avatar asked Sep 12 '25 14:09

pso


1 Answers

It's not possible, however, you can use a workaround to detect similar behavior. For example, if you're dealing with links ( tags), you can listen for the click event and check if the user is using Ctrl (Windows) or Cmd (Mac) to open the link in a new tab:

document.querySelectorAll('a').forEach(link => {
  link.addEventListener('click', (e) => {
    if (e.ctrlKey || e.metaKey) {
      console.log('Link opened in a new tab');
    }
  });
});

This method will capture when a user opens a link in a new tab using the keyboard shortcut.

Alternatively, you can create a custom context menu that captures right-click actions and manage the "Open in New Tab" functionality within your app.

like image 77
CodeGenitor Avatar answered Sep 15 '25 04:09

CodeGenitor