How to detect a click inside of an iframe (cross-domain)? Aka prevent click fraud

后端 未结 6 1779
萌比男神i
萌比男神i 2020-12-03 18:29

I got a warning by my ad system provider about click fraud. No further info, all they are recommending is \"hide the ads for users who click on ads too quickly\'\". I wrote

6条回答
  •  醉话见心
    2020-12-03 19:02

    You cannot detect click events in cross-domain iframe.

    That put, you might have one bad option:

    One of the nearest things you can do is detect that the focus moved from your window to the iframe:

    window.focus(); //force focus on the currenct window;
    window.addEventListener('blur', function(e){
        if(document.activeElement == document.querySelector('iframe'))
        {
            alert('Focus Left Current Window and Moved to Iframe / Possible click!');
        }
    });
    

    http://jsfiddle.net/wk1yv6q3/

    However it's not reliable, loose focus does not mean a click, it could be user moving across the website using TAB.

    Another problem is that, you only detect the first time focus is moved to the iframe, you do not know what user does in there, he can click a million times and you will never know.

提交回复
热议问题