本文介绍了为什么使用target =“_blank”导致Javascript失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆链接使用target =_ blank属性在新窗口中打开。我想附加Google Analytics目标跟踪以点击这些链接。



为此,我尝试在链接上附加一个onclick =pageTracker._trackPageview('/ event / outgoing')属性。



但是我发现,对于具有target =_ blank属性的链接,Javascript onclick事件会被跳过。所以目标没有记录。换句话说,这个链接成功地记录了目标:

 < a href =http://www.yahoo.com onclick =pageTracker._trackPageview('/ event / outgoing')>点击我< / a> 

但这并不是:

<$ p $ 和(将 / edit 添加到网址以编辑代码)。 您可能需要关闭您的弹出式窗口拦截器

 <!DOCTYPE html PUBLIC  -  // W3C // DTD XHTML 1.0 Transitional // EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns =http://www.w3.org/1999/xhtmlxml:lang =enlang =en>
< head>
< title>沙盒< /标题>
< meta http-equiv =Content-typecontent =text / html; charset = utf-8/>
< script> var clickCount = 0;< / script>
< / head>
< body>
< a href =http://www.google.comtarget =_ blankonclick =document.getElementById('dbg')。innerHTML = ++ clickCount>
打开新的
< / a>
< div id =dbg>< / div>
< / body>
< / html>

点击链接打开一个新窗口更新div链接被点击的次数。在IE6,IE7,FF2,FF3.5,Chrome 2,Chrome 3,Opera 9上进行了测试。


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.

To do this, I tried attaching an onclick="pageTracker._trackPageview('/event/outgoing')" attribute to the links.

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:

<a href="http://www.yahoo.com" onclick="pageTracker._trackPageview('/event/outgoing')">Click me</a>

But this does not:

<a href="http://www.yahoo.com" target="_blank" onclick="pageTracker._trackPageview('/event/outgoing')">Click me</a>

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):

function attach_goal_tracking() {
    var links = document.getElementsByClassName("buyTicketsLink");
    for(var i=0; i<links.length; i++) {
        links[i].onclick = record_goal;
        }
}

function record_goal(e) {
    e.stop();
    pageTracker._trackPageview('/event/outgoing');
}

But when I add to the record_goal function to open the link...

function record_goal(e) {
    e.stop();
    pageTracker._trackPageview('/event/outgoing');
    var newWindow = window.open(this.getAttribute('href'), '_blank');
    newWindow.focus();
    }

...then it fails to track the goal.

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.

解决方案

There might be an issue with popup blockers (I'm thinking Googlebar) stopping the window from opening, and (with the presence of onclick) preventing the onclick code from running. For example see this and this similar sounding issues other people are having.

Or it might simply be that the click handler code is throwing an error that then prevents the rest of the code from completing. See my comment to your question re how you are binding the events. e.stop() may be failing in IE.

Your hypothesis however is false. target="_blank" and onclick="..." do work together just fine.

Testcase in point: http://jsbin.com/anahe (add /edit to the url to edit the code). You may need to turn off your popup blocker(s):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Sandbox</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script>var clickCount= 0;</script>
</head>
<body>
  <a href="http://www.google.com" target="_blank" onclick="document.getElementById('dbg').innerHTML = ++clickCount">
     open new
  </a>
  <div id="dbg"></div>
</body>
</html>

Clicking on the link opens a new window and updates the div with the number of times the linked has been clicked. Tested this in IE6, IE7, FF2, FF3.5, Chrome 2, Chrome 3, Opera 9.

这篇关于为什么使用target =“_blank”导致Javascript失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:51