[英]Delay href click, but not with SetTimeout
I am trying to track the outbound links via google analytics, and Google suggests using this : 我正在尝试通过Google Analytics(分析)跟踪出站链接, Google建议使用以下方法 :
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
_gat._getTrackerByName()._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100);
}
</script>
Which is fine, except, my outbound links are to be opened in a new tab, and I am (naturally) using a target="_blank"
for that.. 很好,除了我的出站链接将在新选项卡中打开,而且我(自然地)为此使用target="_blank"
。
but, the setTimeout
method takes that away, and opens the link in the same page.. 但是, setTimeout
方法将其setTimeout
,并在同一页面中打开链接。
I've tried using window.open()
but I am worried that it will be blocked by browsers.. 我尝试使用window.open()
但担心它会被浏览器阻止。
So, is there anyway that I can execute this js function, and delay the click for just a little while? 因此,无论如何,我可以执行此js函数,并将单击延迟一会儿吗? (100ms as google suggests)? (如谷歌建议的100毫秒)?
Thanks. 谢谢。
Simple, just remove the setTimeout() part of it. 很简单,只需删除其中的setTimeout()部分。 So all it does is call the _trackEvent function. 因此,它所做的就是调用_trackEvent函数。
Your links should execute both the javascript function and open the new window, if you just keep them something like: 您的链接应该同时执行javascript函数并打开新窗口,如果您只将它们保留为:
<a href="example.com" target="_blank" onclick="recordOutboundLink(foo, bar)">Click here</a>
<script type="text/javascript">
function recordOutboundLink(category, action) {
_gat._getTrackerByName()._trackEvent(category, action);
}
</script>
Ok to evolve the answer above here is a Jquery plugin that can provide listen a selection of links (based on your own criteria) and provide you a method for callback to them. 确定上面的答案是可以的,这里是一个Jquery插件,可以提供(根据您自己的条件)侦听链接的选择并为您提供回调它们的方法。
So in the OP's case the setup could look like: 因此,在OP的情况下,设置可能如下所示:
<a href="http://www.google.co.uk" target="_blank" data-category="Outbound">Google</a>
$(document).ready(function() {
$('a').trackOutBound(null,function() {
var category= $(this).data('category');
_gat._getTrackerByName()._trackEvent(category, $(this).attr('href'));
});
});
I use this to keep the function waiting before continuing with the default browser behavior: 我使用它来使函数保持等待,然后继续默认的浏览器行为:
var wait_until = new Date().getTime() + 500;
while (new Date().getTime() < wait_until) {
//Do nothing, wait
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.