开发者

Link won't open in new window even with the correct code!

I'm having a small but annoying problem with trying to open a link in a new window. I've built a site and I've got Google Analytics tracking installed. I've tracked click throughs to external sites as events and have put in the correct and it all tracks properly. For each of these links I've put in the target="_blank" but for some reason the link doesn't open in a new window! Is there somet开发者_如何学JAVAhing that I am doing wrong? Code below:

<a onclick="recordOutboundLink(this, 'Outbound Links', 'Link');return false;" href="http://www.mylink.com" target="_blank">Myink</a></span></li>

Is it something to do with the order it's in? I just can't seem to get it to work!

Thanks in advance.

As requested here is the code for recording the links:

<script type="text/javascript">
function recordOutboundLink(link, category, action) {
  try {
    var pageTracker=_gat._getTracker("UA-XXXXXX-XX");
    pageTracker._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}


The problem is that your recordOutboundLink function is using JavaScript to redirect instead of just letting the link perform it's default operation. That's the correct behavior if you want the link to open in the same tab/window because it uses a timeout to ensure the data's been sent to GA before the new page is loaded. To get the code working you need to use the following function:

    <script type="text/javascript">
        function recordOutboundLink(link, category, action) {
            try {
                var pageTracker=_gat._getTracker("UA-XXXXXX-XX");
                pageTracker._trackEvent(category, action);
            }catch(err){}
        }
    </script>

You also need to remove the return false bit of code that stops the link performing it's normal behavior:

<a onclick="recordOutboundLink(this, 'Outbound Links', 'Link')" href="http://www.mylink.com" target="_blank">Myink</a>


document.location means you're changing the current page URL to another one. Plus you are using a return false statement, meaning you are preventing the default event associated wxith the link. Thus, your target="_blank" argument is not checked.

You need to remove the document.location and the return false statements. Then your link will be able to act as you wish

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜