Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do websites end up breaking middle-click functionality?

By default, middle-click will open a link in a new tab.

Some sites end up breaking this functionality. Middle-click ends up being the same as left-click.

Why does this happen? Is it because they program functionality for a click event, and erroneously have it apply to all clicks instead of just left-click?

Is the problem solved by explicitly giving behavior to a middle-click? Or by making the existing click behavior code apply more narrowly?

like image 556
John Bachir Avatar asked Mar 20 '15 22:03

John Bachir


People also ask

How does middle-click work?

If you middle-click a folder, it will open in a new tab. This mimics the behavior of popular web browsers. If you middle-click a file, it will open the file, just as if you had double-clicked.

What happens when you click the middle mouse button?

The scroll wheel that is located in the middle of the mouse is used to scroll up and down on any page without using the vertical scroll bar on the right hand side of a document or webpage. The scroll wheel can also be used as a third button on the mouse.

What is a middle-click?

middle-click ​Definitions and Synonyms ​verb. DEFINITIONS1. 1. to press the button or wheel in the middle of a computer mouse with your finger, in order to make it possible to do something on a computer. In some programs, you can use middle-click for program-specific features.

What does middle mouse button do in Chrome?

That wheel in the middle of your mouse is good for more than just scrolling—in fact, the button can do quite a bit. Your middle-click mouse button—also known as your scroll wheel—can be an effective tab management tool in both Chrome and Firefox.


1 Answers

Overview:

It is very easy to inadvertently prevent middle-click functionality in WebKit browsers. In WebKit browsers such as Chrome, Safari, and modern Opera, middle-clicking on a link fires a preventable click event. This behavior differs from Firefox and IE, where middle-clicking a link will not fire a click event.

There is actually an open bug report from 2008 on this issue, which unfortunately does not appear to have gone anywhere in the last 7 years.

Problem Code:

So let's take a look at how easy it is to break this functionality in WebKit browsers while doing something completely ordinary.

var link = document.getElementById('link');
link.addEventListener('click', function(e) {
    e.preventDefault();
    alert('You clicked!');
});
<a id="link" href="http://www.example.com/">example.com</a>

Code similar to this is common when using a link to to preform other tasks, such as preventing going to the link to load the content by AJAX instead. However given WebKit's middle-click behavior, this will also prevent the native middle-click behavior.

A Solution for Developers:

It is possible to resolve the inconsistency by detecting which mouse button was pressed using the non-standard but widely-implemented MouseEvent.which property. The following code will allow the middle-click function to behave as normal.

Better Code:

var link = document.getElementById('link');
link.addEventListener('click', function(e) {
    if (e.which === 2) {
        return;
    }
    e.preventDefault();
    alert('You clicked!');
});
<a id="link" href="http://www.example.com/">example.com</a>

Unfortunately, since preserving normal behavior requires additional knowledge and implementation from the developer, unless the WebKit bug is fixed, websites that break this functionality will doubtlessly continue. More-than-likely many developers do not even know that this function of the middle-click exists, let alone testing for compatibility.

A Solution for Users:

This problem has prompted the creation of at least a few different browser extensions aimed at fixing the problem. Here are the ones listed for Chrome in the bug report mentioned above.

  • Fix Chrome middle click behavior
  • middle button new tab
  • middle click mini

Conclusion:

So yes, those websites that do handle this behavior well are using some extra code to preserve the middle-click functionality in WebKit browsers.

like image 68
Alexander O'Mara Avatar answered Sep 30 '22 08:09

Alexander O'Mara