javascript - link Click tracking does not work on Safari browser -


i have basic html page has links point different site. want track clicks. doing sending 0 pixel image call on click event of link without returning false on click event.

the same works fine on browsers except safari(on windows os).

when link clicked using javascript delay redirect , send image request on server , log click on server side. have tried increasing delay no success... trackers work gr8 on browsers except safari not sent request @ all.

i dont know why possibly safari waits complete js executed before making request , after whole js executed gets redirected....

=========================================================

<html>   <head>     <script type="text/javascript">       function logevent(){     image = new image(1,1);     image.onload=function(){alert("loaded");};     image.onload=function(){alert("error");};     image.src='http://#path_to_logger_php#/log.php?'+math.random(0, 1000) + '=' + math.random(0, 1000);                  pauseredirect(500);       }        function pauseredirect(millis){     var date = new date();     var curdate = null;     {curdate = new date();}     while(curdate-date < millis);       }     </script>      </head>   <body>             <a href="http://www.google.com" onclick="logevent(); return true;">site 1</a><br/>     <a href="http://www.yahoo.com" onclick="logevent(); return true;">site 2</a><br/>   </body> </html> 

=========================================================

code works in chrome, firefox, ie , opera. not work on safari only..... clues....

i had same issue webkit browsers. in others need new image().src = "url", , browser send request when navigating new page. webkit stop request before it's sent when navigate new page right after. tried several hacks inject image document , force re-paint through img.clientheight. don't want use event.preventdefault, since causes lot of headaches when link has target="_blank", form submit, etc. ended using synchronous xmlhttprequest browsers supporting cross origin resource sharing, since send request server though don't read response. synchronous request has unfortunate side-effect of locking ui-thread while waiting response, if server slow page/browser lock until receives response.

var supportscrossoriginresourcesharing = (typeof xmlhttprequest != "undefined" && "withcredentials" in new xmlhttprequest()); function logevent() {     var trackurl = 'http://#path_to_logger_php#/log.php?'+math.random(0, 1000) + '=' + math.random(0, 1000);     if(supportscrossoriginresourcesharing) {         xhrtrack(trackurl);     } else {         imgtrack(trackurl);     } }  function xhrtrack(trackurl) {     var xhr = new xmlhttprequest();     xhr.open("get", trackurl, false);     xhr.onreadystatechange = function() {         if(xhr.readystate >= this.opened) xhr.abort();     }     try { xhr.send() } catch(e) {} }  function imgtrack(trackurl) {     var trackimg = new image(1,1);     trackimg.src = trackurl; } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -