[英]Delay href click, but not with SetTimeout
我正在尝试通过Google Analytics(分析)跟踪出站链接, Google建议使用以下方法 :
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
_gat._getTrackerByName()._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100);
}
</script>
很好,除了我的出站链接将在新选项卡中打开,而且我(自然地)为此使用target="_blank"
。
但是, setTimeout
方法将其setTimeout
,并在同一页面中打开链接。
我尝试使用window.open()
但担心它会被浏览器阻止。
因此,无论如何,我可以执行此js函数,并将单击延迟一会儿吗? (如谷歌建议的100毫秒)?
谢谢。
很简单,只需删除其中的setTimeout()部分。 因此,它所做的就是调用_trackEvent函数。
您的链接应该同时执行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>
确定上面的答案是可以的,这里是一个Jquery插件,可以提供(根据您自己的条件)侦听链接的选择并为您提供回调它们的方法。
因此,在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'));
});
});
我使用它来使函数保持等待,然后继续默认的浏览器行为:
var wait_until = new Date().getTime() + 500;
while (new Date().getTime() < wait_until) {
//Do nothing, wait
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.