javascript - How do websites end up breaking middle-click functionality? -
by default, middle-click open link in new tab.
some sites end breaking functionality. middle-click ends being same left-click.
why happen? because program functionality click event, , erroneously have apply clicks instead of left-click?
is problem solved explicitly giving behavior middle-click? or making existing click behavior code apply more narrowly?
overview:
it easy inadvertently prevent middle-click functionality in webkit browsers. in webkit browsers such chrome, safari, , modern opera, middle-clicking on link fires preventable click
event. behavior differs firefox , ie, middle-clicking link not fire click
event.
there an open bug report 2008 on issue, unfortunately not appear have gone anywhere in last 7 years.
problem code:
so let's take @ how easy break functionality in webkit browsers while doing 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 common when using link to preform other tasks, such preventing going link load content ajax instead. given webkit's middle-click behavior, prevent native middle-click behavior.
a solution developers:
it possible resolve inconsistency detecting mouse button pressed using non-standard widely-implemented mouseevent.which
property. following code allow middle-click function behave 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 , implementation developer, unless webkit bug fixed, websites break functionality doubtlessly continue. more-than-likely many developers not know function of middle-click exists, let alone testing compatibility.
a solution users:
this problem has prompted creation of @ least few different browser extensions aimed @ fixing problem. here ones listed chrome in bug report mentioned above.
conclusion:
so yes, websites handle behavior using code preserve middle-click functionality in webkit browsers.
Comments
Post a Comment