如何检测iframe内部的点击(跨域)?又名防止点击欺诈 [英] How to detect a click inside of an iframe (cross-domain)? Aka prevent click fraud

查看:570
本文介绍了如何检测iframe内部的点击(跨域)?又名防止点击欺诈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的广告系统提供商收到了关于点击欺诈的警告。没有进一步的信息,他们所推荐的只是隐藏用户点击广告过快的广告。我写了一段JS脚本,在点击时用N秒隐藏所有DIV(使用cookie),但这种解决方案不起作用,因为内部内容(带广告)由调用和呈现的JS脚本生成来自外部服务器的内容(如您对广告系统的期望)。因此,当考虑到跨域安全性时,它有点像Catch 22.我如何检测由外部JS和iframe渲染的内容中的点击内部(本地定义)?



示例:

 < div class =ad-class > <! - 本地定义的div  - > 
< div id =my-id> < / DIV> <! - 在提供商的系统中识别我的广告 - >
< script>
var foo = blah //声明广告尺寸和内容
//并在#my-id中呈现上下文广告DIV
< / script>
< / div>

如果全部是本地的,解决方案将很容易,因为内部div会继承父类(ad -类)。在跨域的情况下,这是无效的。任何提示,帅哥?

解决方案

您无法检测跨域iframe中的点击事件。

b

这就是说,你可能有一个不好的选择:



你可以做的最近的事情之一是检测焦点从你的窗口移动到iframe:

  window.focus(); //强调关注窗口; 
window.addEventListener('blur',function(e){
if(document.activeElement == document.querySelector('iframe'))
{
alert('Focus左移当前窗口并移动到Iframe /可能点击!');
}
});

http://jsfiddle.net/wk1yv6q3/

然而,它不可靠,松散的焦点并不意味着点击,它可能是用户移动网站使用 TAB



另一个问题是,您只会在第一次检测焦点移动到iframe时,不知道用户在那里,他可以点击一百万次,你永远不会知道。


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 a piece of JS script that hides all DIVs with ads for N seconds (using cookie) when clicked on, but this solution does not work as the "inner" content (with ads) is generated by an JS script that calls and renders the content from external server (as you would expect from an ad system). So, when one takes the cross-domain security into account it is kinda Catch 22. How can I detect a click inside a DIV (locally defined) of which content is rendered by an external JS and in iframe?

Example:

<div class="ad-class"> <!-- locally defined div -->
   <div id="my-id"> </div> <!-- identifies my ad in the provider's system -->
   <script>
      var foo = blah // declares the ad dimensions and stuff
      //  and renders the contextual ad in #my-id DIV
   </script>
</div>

Were it all local, solution would be easy as the internal div would inherit the parent class ("ad-class"). In case of cross-domain, this is not valid. Any tips, dudes?

解决方案

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.

这篇关于如何检测iframe内部的点击(跨域)?又名防止点击欺诈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆