Send information about clicked link to the server before redirect

前端 未结 5 858
感动是毒
感动是毒 2020-12-21 05:20

We\'re creating a click tracking app, that builds heatmaps. I\'m writing a script which users are suppose to insert into their pages for tracking to work.

It works

5条回答
  •  天命终不由人
    2020-12-21 05:59

    var globalStopper = true;
    
    $(document).on('click', function (e) {
        if (globalStopper === false)
            return true; //proceed with click if stopper is NOT set
        else {
            globalStopper = false; //release the breaks
    
            $.ajax({
                //blahblah
                complete: function (xhr, status,) {
                    $(elem).click(); //when ajax request done - "rerun" the click
                }
            });
            return false; //DO NOT let browser process the click
        }
    });
    

    Also, instead of adding image, try adding script. And then add the script to the HEAD section. This way the browser will "wait" until it's loaded.

     $(document).on('click', function (e) {
         var scriptTag = document.createElement("script");
         scriptTag.setAttribute("type", "text/javascript");
         scriptTag.setAttribute("src", url);
         document.getElementsByTagName("head")[0].appendChild(scriptTag);
         return true;
     }
    

提交回复
热议问题