javascript - window.open avoid pop-up blocker from function handler -


i using google analytics external link tracking code:

var trackoutboundlink = function(url) {      ga('send', 'event', 'outbound', 'click', url, {'hitcallback':             function () {                 window.open ( url, "_blank" );                 //document.location = url;             }         });     } 

which called from:

<a target="_blank" onclick="trackoutboundlink('http://www.example.com'); return false;" href="http://www.example.com">full brochure</a>  

however, because window.open not called direct user interaction browser pop-up blocker engaged.

how can achieve target new tab pop-up blocker not engaged? thanks

i'd unobtrusively. so:

<a target="_blank" href="http://www.example.com" rel="external">...</a> 

then js:

/* find external links in document... */ var ext_links = document.queryselectorall('a[rel=external]');  /* iterate through each external link in document... */ [].foreach.call(ext_links ,function(a){     /* each external link, assign onclick callback: */     a.addeventlistener('click',function(){         /* google tracking code here, try this: */         var url = a.href;         ga('send', 'event', 'outbound', 'click', url);     }) }); 

notice not using event.preventdefault() cancel default action of hyperlink, should still open in new window/tab, after 'click' function has run.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -