window.open 避免函数处理程序中的弹出窗口拦截器
window.open avoid pop-up blocker from function handler
我正在使用 Google Analytics 外部 link 跟踪代码:
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
function () {
window.open ( url, "_blank" );
//document.location = url;
}
});
}
调用自:
<a target="_blank" onclick="trackOutboundLink('http://www.example.com'); return false;" href="http://www.example.com">Full Brochure</a>
但是,由于 window.open 不是直接从用户交互中调用的,因此浏览器弹出窗口拦截器已启用。
我怎样才能做到这一点,目标是一个新标签,但弹出窗口拦截器没有被激活?谢谢
我会低调地做这件事。所以:
<a target="_blank" href="http://www.example.com" rel="external">...</a>
然后是JS:
/* Find all external links in the document... */
var ext_links = document.querySelectorAll('a[rel=external]');
/* Iterate through each external link in the document... */
[].forEach.call(ext_links ,function(a){
/* For each external link, assign an onclick callback: */
a.addEventListener('click',function(){
/* Google tracking code here, try this: */
var url = a.href;
ga('send', 'event', 'outbound', 'click', url);
})
});
请注意,我没有使用 event.preventDefault() 来取消超链接的默认操作,因此在 'click' 函数执行后,它仍应立即在新的 window/tab 中打开运行.
我正在使用 Google Analytics 外部 link 跟踪代码:
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
function () {
window.open ( url, "_blank" );
//document.location = url;
}
});
}
调用自:
<a target="_blank" onclick="trackOutboundLink('http://www.example.com'); return false;" href="http://www.example.com">Full Brochure</a>
但是,由于 window.open 不是直接从用户交互中调用的,因此浏览器弹出窗口拦截器已启用。
我怎样才能做到这一点,目标是一个新标签,但弹出窗口拦截器没有被激活?谢谢
我会低调地做这件事。所以:
<a target="_blank" href="http://www.example.com" rel="external">...</a>
然后是JS:
/* Find all external links in the document... */
var ext_links = document.querySelectorAll('a[rel=external]');
/* Iterate through each external link in the document... */
[].forEach.call(ext_links ,function(a){
/* For each external link, assign an onclick callback: */
a.addEventListener('click',function(){
/* Google tracking code here, try this: */
var url = a.href;
ga('send', 'event', 'outbound', 'click', url);
})
});
请注意,我没有使用 event.preventDefault() 来取消超链接的默认操作,因此在 'click' 函数执行后,它仍应立即在新的 window/tab 中打开运行.