title: HTML Link javascript execute on right click or shift click (open in a new window/tab)
body: <p>Opening a JavaScript-based link in a new window/tab.</p>
  
  <p>I have a link </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;a href='/page' onclick='trackClick()'&gt;Page&lt;/a&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>What I believe is happening here is that the trackClick() function may not have enough time to execute before browser redirects to a new page. (BTW: Am I wrong here?)</p>
  
  <p>To workaround that, I have changed the html to </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;a href='#' onclick='trackClick('/page')'&gt;Page&lt;/a&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>where trackClick function would redirect to a next page after it has sent a signal to analytics endpoint to avoid racing condition.</p>
  
  <p>As a result, "shift+click" or "right+click open in a new window" now will not open the the <em>/page</em> in a new window/tab.</p>
  
  <p>What's the best way to overcome this?
  How likely for the browser to not wait for the onclick event to finish before redirecting to a next page?</p>
  
  <p>thank you</p>