延迟 href 单击,但不使用 SetTimeout
Delay href click, but not with SetTimeout
我正在尝试通过谷歌分析跟踪出站链接,谷歌建议使用这个:
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
_gat._getTrackerByName()._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100);
}
</script>
这很好,除了,我的出站链接将在新选项卡中打开,我(自然)为此使用target="_blank"
。
但是,setTimeout
方法将其删除,并在同一页面中打开链接。
我尝试使用window.open()
但我担心它会被浏览器阻止。
那么,无论如何我可以执行这个js函数,并将点击延迟一会儿吗?(谷歌建议的100毫秒)?
谢谢。
我已经在SO上查看了其他类似的问题,但是它们不涉及在新选项卡/窗口中打开。
好的,上面的答案是一个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'));
});
});
很简单,只需删除其中的 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>
我使用它来保持函数等待,然后再继续默认浏览器行为:
var wait_until = new Date().getTime() + 500;
while (new Date().getTime() < wait_until) {
//Do nothing, wait
}
相关文章:
- setInteval vs setTimeout
- 在单击href链接的同时下载文件
- 如果href包含X,请更改href
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 锚点元素的href属性自动更改
- 锚点元素不't使用svg时,请打开EDGE上的href
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- Href:当前DIV中的目标ID
- setTimeout可以与闭包内的函数一起使用吗
- Javascript setTimeout for an array
- 刷新后,setTimeout将工作或不工作
- href属性内的javascript函数
- ng绑定和ng href问题.ng href未从控制器加载数据
- 继续使用javascript中的setTimeout
- 如何将href设置为不同的值
- setTimeout函数能否在其前面的代码执行之前激发
- 延迟 href 单击,但不使用 SetTimeout
- setTimeout window.open 不能接受这个.href
- setTimeout和window.位置(Location .href)不工作