简体   繁体   English

如何找出使用Javascript通过我的网站点击过3次的用户

[英]How to find out user clicked 3 times through my website with Javascript

Is there any way in JavaScript how to find out user clicked through the same domain 2 or more times? JavaScript中有什么方法可以找出通过相同域两次或更多次点击的用户?

I need to popup a window after user clicked anywhere on the site for 3 times. 用户单击网站上的任意位置3次后,我需要弹出一个窗口。 I know how to do it after one click - with document.referrer or addEventListener , but then I'm lost. 我知道如何一键操作-使用document.referreraddEventListener ,但是我迷路了。

I need something that will capture all click events (not only links) and count them. 我需要一些可以捕获所有点击事件(不仅是链接)并对其进行计数的东西。

Sure. 当然。 You need to store a list of users' click events, either in a cookie, or in a server-side data store. 您需要在Cookie或服务器端数据存储区中存储用户点击事件的列表。 On every recorded click, increment the count by one, and do your thing when the number hits 3. 在每次记录的点击中,将计数加1,然后在数字达到3时执行操作。

Try using session cookies to store state between pages -- they're fast, pretty widely compatible, and will zero out when the browser shuts down, to keep from spamming your users' cookie jars. 尝试使用会话Cookie在页面之间存储状态-它们是快速的,具有广泛的兼容性,并且在浏览器关闭时将为零,以防止向用户的Cookie罐发送垃圾邮件。

I tried this and it worked fine: 我尝试了一下,效果很好:

window.onload = function() {
    var clicked = readCookie('popunder');
    if(clicked == null) {
        clicked = 0;
    }

    var allLinks = document.getElementsByTagName("a");
    for(i=0;i<=allLinks.length;i++) {
        allLinks[i].addEventListener("click",countClicks,true);
    }

    function countClicks() {           
        if(clicked == 2) {
            popunder(); //something to do
        } else {
            clicked++;
            doCookie('popunder', clicked, 1);
            alert(clicked);
        }
    }

    function popunder() { alert('thats 3 clicks!'); }

    function doCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        } else {
            var expires = "";
        }
        document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
        var readName = name + "=";
        var cSplit = document.cookie.split(';');
        for(var i=0;i < cSplit.length;i++) {
            var sc = cSplit[i];
            while (sc.charAt(0)==' ') sc = sc.substring(1,sc.length);
            if (sc.indexOf(readName) == 0) return sc.substring(readName.length,sc.length);
        }
        return null;
    }
}

Thanks for all your advice. 感谢您的所有建议。 I tried this code. 我尝试了这段代码。 But after the refresh the clicked variable goes to 0 again. 但是刷新后, 单击的变量再次变为0。 I need to save every new value of clicked into cookie (or whatever else), so its number will rise with every click on link on page. 我需要将点击的每个新值都保存到Cookie(或其他任何值)中,以便其每次点击页面上的链接时其数量都会增加。 Is it possible to change value of the variable in cookie this way? 是否可以通过这种方式更改Cookie中变量的值?

window.onload = function(){ window.onload = function(){

var **allLinks** = document.getElementsByTagName("a");

var **clicked** = 0;

**doCookie**('popunder',clicked,1);

for(i=0;i<=allLinks.length;i++){
    allLinks[i].addEventListener("click",countClicks,true);
}

function **countClicks**(){           
        if(clicked == 3){
            popunder(); //something to do
        }
        else{
            alert(readCookie('popunder'));
            return clicked++;
        }
}

function **doCookie**(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function **readCookie**(name) {
    var readName = name + "=";
    var cSplit = document.cookie.split(';');
    for(var i=0;i < cSplit.length;i++) {
        var sc = cSplit[i];
        while (sc.charAt(0)==' ') sc = sc.substring(1,sc.length);
        if (sc.indexOf(readName) == 0) return sc.substring(readName.length,sc.length);
    }
        return null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定用户是否点击了我网站上的 Facebook 赞按钮? - How can I find out if a user has clicked the Facebook Like button on my website? 如何找出用户在触摸设备上点击了哪个元素 - How to find out which element the user clicked on the touch device 如果用户使用javascript从其他选项卡中的Facebook注销,如何在我的网站中收到通知? - How can I get notified in my website if user log out from facebook in other tab using javascript? 如果我的网站页面在浏览器窗口中失去焦点,如何将用户重定向到 Javascript/jQuery 中的页面? - How to redirect user to a page in Javascript/jQuery if my website page gets out of focus in the browser window? 如何在我的网站中找到JavaScript重定向 - How to find a javascript redirection in my website JavaScript:如何确定用户浏览器是否为 Chrome? - JavaScript: How to find out if the user browser is Chrome? 有没有办法在javascript中找出某个字符串在数组中的次数? - Is there a way in javascript to find out how many times a certain string is in an array? 跟踪并显示用户单击我网站上的按钮的次数 - Track and display how many times users have clicked a button on my website 查看用户点击图片的次数 - Seeing how many times a user clicked on an image 如何检测用户何时单击了我的应用程序的 webview 中加载的网站的特定按钮? - How can I detect when a user clicked a particular button of a website loaded inside the webview of my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM