دنبال کننده ها

۱۳۹۶ دی ۱۰, یکشنبه

javascript - Supress click event if it opens a new tab or window

[ad_1]



Sometimes on my web app I'd like to supress the click event if this click was supposed to open a new window or tab. For example, my single page application features links to another content. They work great, but there's one issue. If user clicks on such link, making a special click to open a new tab/window (let's say cmd ⌘ + click on Mac), I don't want to fire the event.



Normally I would do this:



<a href="http://stackoverflow.com/music">Music</a>
<a href="http://stackoverflow.com/games">Games</a>
<a href="http://stackoverflow.com/about-us">About Us</a>
<a href="http://stackoverflow.com/contact">Contact</a>


And some jQuery magic:



$("a[href]").click(event => );


It works as expected - whenever the mouse + keyboard shortcut has been used to open new tab/window, the current tab is not affected and the new tab/window appears.



The problem here is that it's an assumption that cmd ⌘ + click or shift + click are actually the shortcuts. They can be different for different browsers and systems.



So here's the question I'd like to ask: is there any reliable way to detect tab/window opening request in JavaScript? I find this functionality rather important. At least important enough to care.




[ad_2]

لینک منبع