Hacker News new | past | comments | ask | show | jobs | submit login

The best option I could come up with is to always open external links in a new tab, because that way the click handling code always has time to run.

Ugh. That's awful.

The alternative is to hijack the click event, wait for say 100ms, and then load the new page via JavaScript, but that also break the open in a new tab functionality.

Is this true? ISTM that if you just have:

    <!doctype html>
    <p>Here's a <a href="http://www.google.com">link</a>.</p>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $('a').not('[href*="mysite.com"]').click(function() {
        alert('do some analytics stuff');
        // only after that's done do we let the navigation proceed
      });
    </script>
Your analytics stuff still gets done, and open in new tab still works. (As written, the analytics doesn't run when opening in a new tab, but ISTM you can just capture some mouse events to work in that case as well.)



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: