Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command-click doesn't open a new tab, but middle-click does

Tags:

On my website, which is a one-page JS site using Sammy.js and jQuery, when I middle-click a link with a mouse, the link opens in a new tab. But when I command-click on a Mac, it doesn't. This happens in both Firefox and Chrome, so I assume it must be according to spec in some way.

This happens on a Macbook Air (so trackpad + command button). Most sites work just fine though, with command-click being identical to normal middle-click.

Try it out yourself: https://circleci.com. Command-click between "about", "home" and "contact" and you should experience the problem - they don't open in new tabs.

like image 697
Paul Biggar Avatar asked May 18 '12 22:05

Paul Biggar


People also ask

How do I get the middle mouse button to open in a new tab?

Select 'Mouse' and 'middle mouse button' in the tool. Select 'Advanced Click' -> Click + Ctrl + Shift (Ctrl + Shift + Left Click is the keyboard shortcut to open link in a new tab which we assign to the middle mouse button) You can also do this assignment just for a specific application e.g. Chrome.

How do I make a new tab open when I click on it?

To quickly open a link in a new tab on Google Chrome, hold down the control button while clicking on it with your mouse.

What does middle clicking a tab do?

The middle mouse button is basically used to open and close tabs, but it can do so much more. The middle mouse button (which is the scroll wheel on most mice today) is basically used for two purposes on the web: first, open links in new tabs, and second, close open tabs.


1 Answers

Speculating here, but will confirm later from a Mac. This has been confirmed to be working on a Mac.

Win ctrl+click or a Mac command+click gets picked up by a "normal" click listener, just like a click with any other modifier key (alt+click, shift+click etc).

This is particularly confusing since a ctrl+click on a Mac gets interpreted as a right-click on OS level. Command-click, on the other hand, is not interpreted as a middle-click but rather is a browser preference.

Assuming that you do not have in-site functionality that specifically relies on modified clicks, it would be appropriate to exclude such events from click listeners, and instead allow for them to bubble up to be natively handled by the browser. Given the experience of someone in the similar situation, you should be able to add the following to click handlers (likely a delegate on library level as pointed out by Brilliand):

if (e.metaKey || e.ctrlKey) return; 

When added at the beginning of the handler with e referring to a current click event, this should circumvent any following e.preventDefault();

Update:

It actually works! In this rather minimalistic fiddle, I am able to recognize when command-clicked or control-clicked, so as to avoid executing the rest of the click handler which includes ajax-fetching the content and e.preventDefault();. This allows for a command-click to be handled "as intended" on a Mac, i.e. opening the link in a new tab.

With this finding in mind, these lines should now read

if (e.isDefaultPrevented() || e.metaKey || e.ctrlKey) {     return; } 
like image 63
Oleg Avatar answered Oct 01 '22 18:10

Oleg