Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does using target="_blank" cause Javascript to fail?
    text
    copied!<p>I have a bunch of links that use a target="_blank" attribute to open in a new window. I want to attach Google Analytics goal tracking to clicks of these links. </p> <p>To do this, I tried attaching an onclick="pageTracker._trackPageview('/event/outgoing')" attribute to the links. </p> <p>But I discovered that for links with a target="_blank" attribute, the Javascript onclick event gets skipped. So the goal is not recorded. In other words, this link successfully records the goal:</p> <pre><code>&lt;a href="http://www.yahoo.com" onclick="pageTracker._trackPageview('/event/outgoing')"&gt;Click me&lt;/a&gt; </code></pre> <p>But this does not:</p> <pre><code>&lt;a href="http://www.yahoo.com" target="_blank" onclick="pageTracker._trackPageview('/event/outgoing')"&gt;Click me&lt;/a&gt; </code></pre> <p>Does anyone know why this might be occurring? Assuming there isn't an easy solution, I assume I'll have to use Javascript to solve the problem. The following code successfully records a goal (but does not open the link):</p> <pre><code>function attach_goal_tracking() { var links = document.getElementsByClassName("buyTicketsLink"); for(var i=0; i&lt;links.length; i++) { links[i].onclick = record_goal; } } function record_goal(e) { e.stop(); pageTracker._trackPageview('/event/outgoing'); } </code></pre> <p>But when I add to the record_goal function to open the link...</p> <pre><code>function record_goal(e) { e.stop(); pageTracker._trackPageview('/event/outgoing'); var newWindow = window.open(this.getAttribute('href'), '_blank'); newWindow.focus(); } </code></pre> <p>...then it fails to track the goal.</p> <p>Can anyone tell me why this might be, and what I should do to get around this problem? FYI I'm using Prototype for Javascript.</p>
 

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