Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can examine the <code>ctrlKey</code>, <code>shiftKey</code>, and <code>metaKey</code> properties of the event object. If either is true, the key control, shift, or meta (Apple's command) key is being held and you should allow the default link action to proceed. Otherwise, you use <code>preventDefault</code> to stop the link action and handle it with javascript instead.</p> <p>Add <code>target="_blank"</code> to your anchor markup, so the default link behavior is opening a new tab. Otherwise it will open on top of the current page (that may be desired).</p> <p>Here's the javascript, either way:</p> <pre><code>document.getElementById("mylink").onclick = function(evnt) { if ( evnt.ctrlKey || evnt.shiftKey || evnt.metaKey || // apple (evnt.button &amp;&amp; evnt.button == 1) // middle click, &gt;IE9 + everyone else ){ return; } evnt.preventDefault(); alert("clicked"); return false; } </code></pre> <p>Fiddle: <a href="http://jsfiddle.net/6byrt0wu/">http://jsfiddle.net/6byrt0wu/</a></p> <p><strong>Documentation</strong></p> <ul> <li>MDN Events - <a href="https://developer.mozilla.org/en-US/docs/Web/API/Event">https://developer.mozilla.org/en-US/docs/Web/API/Event</a></li> <li><code>Event.ctrlKey</code> - <a href="https://developer.mozilla.org/en-US/docs/Web/API/event.ctrlKey">https://developer.mozilla.org/en-US/docs/Web/API/event.ctrlKey</a></li> <li><code>Event.shiftKey</code> - <a href="https://developer.mozilla.org/en-US/docs/Web/API/event.shiftKey">https://developer.mozilla.org/en-US/docs/Web/API/event.shiftKey</a></li> <li><code>Event.metaKey</code> - <a href="https://developer.mozilla.org/en-US/docs/Web/API/event.metaKey">https://developer.mozilla.org/en-US/docs/Web/API/event.metaKey</a></li> <li><code>a</code> tag - <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload