使用 JavaScript 检测点击 Iframe [英] Detect Click into Iframe using JavaScript

查看:39
本文介绍了使用 JavaScript 检测点击 Iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果它是跨域的,则无法判断用户在 iframe 内正在做什么.我想要做的是跟踪用户是否在 iframe 中点击了所有内容.我想象一个场景,在 iframe 顶部有一个不可见的 div 并且 div 然后将点击事件传递给 <代码> iframe.

这样的事情可能吗?如果是,那我该怎么办?iframes 是广告,所以我无法控制使用的标签.

解决方案

这样的事情有可能吗?

没有.您所能做的就是检测进入 iframe 的鼠标,以及可能(虽然不可靠)当它回来时(即尝试计算出指针在其他地方经过广告与在其他地方徘徊之间的差异)广告).

<块引用>

我想象一个场景,iframe 顶部有一个不可见的 div,然后 div 将点击事件传递给 iframe.

不,没有办法伪造点击事件.

通过捕捉 mousedown,您将阻止原始点击到达 iframe.如果您可以确定何时将按下鼠标按钮,您可以尝试将不可见的 div 移开,以便点击可以通过...但也没有在鼠标按下之前触发的事件.

您可以尝试猜测,例如通过查看指针是否已停止,猜测可能即将出现点击.但它完全不可靠,如果你失败了,你只会失去一次点击.

I understand that it is not possible to tell what the user is doing inside an iframe if it is cross domain. What I would like to do is track if the user clicked at all in the iframe. I imagine a scenario where there is an invisible div on top of the iframe and the the div will just then pass the click event to the iframe.

Is something like this possible? If it is, then how would I go about it? The iframes are ads, so I have no control over the tags that are used.

解决方案

Is something like this possible?

No. All you can do is detect the mouse going into the iframe, and potentially (though not reliably) when it comes back out (ie. trying to work out the difference between the pointer passing over the ad on its way somewhere else versus lingering on the ad).

I imagine a scenario where there is an invisible div on top of the iframe and the the div will just then pass the click event to the iframe.

Nope, there is no way to fake a click event.

By catching the mousedown you'd prevent the original click from getting to the iframe. If you could determine when the mouse button was about to be pressed you could try to get the invisible div out of the way so that the click would go through... but there is also no event that fires just before a mousedown.

You could try to guess, for example by looking to see if the pointer has come to rest, guessing a click might be about to come. But it's totally unreliable, and if you fail you've just lost yourself a click-through.

这篇关于使用 JavaScript 检测点击 Iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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