使用 JavaScript 检测对 Iframe 的点击
据我所知,如果是跨域,则无法判断用户在 iframe 内做什么。我想做的是跟踪用户是否在 iframe
中单击过。我想象一个场景,在 iframe
顶部有一个不可见的 div
,并且 div
会将点击事件传递给 <代码> iframe 。
这样的事情可能吗?如果是的话,我该怎么办呢? iframe
是广告,因此我无法控制所使用的标签。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
我和一位同事遇到了与 Brian Trumpsett 类似的问题,并发现该线程非常有帮助。
我们的信息亭在 iframe 内有动画,我们需要跟踪页面活动来设置计时器。
正如此处所建议的,我们现在不是跟踪点击,而是检测每次点击的焦点变化并将其改回来
以下代码在装有 Safari 和 Chrome 的 macOS 上运行正常,但不适用于 FireFox(为什么?):
问题是,在 Windows 上,它既不能与 Chrome 一起运行,也不能与 FireFox 一起运行,因此,我们的信息亭无法正常工作。
你知道为什么它不起作用吗?
您有办法让它在 Windows 上运行吗?
A colleague and I, we have a problem similar to that of Brian Trumpsett and found this thread very helpful.
Our kiosk has animations inside iframes and we need to track the page activity to set a timer.
As suggested here, rather than tracking the clicks, we now detect the focus change at each click and change it back
The following code is Okay on macOS with Safari and Chrome but does not work with FireFox (why?):
The problem is that, on Windows, it works neither with Chrome nor with FireFox and thus, our kiosk is not functional.
Do you know why it is not working ?
Do you have a solution to make it work on Windows ?
如发现:使用 JavaScript 检测对 Iframe 的点击
=>;我们可以使用 iframeTracker-jquery :
As found there : Detect Click into Iframe using JavaScript
=> We can use iframeTracker-jquery :
我的方法与上面 Paul Draper 提出的方法类似。但是,它在 Firefox 中不起作用,因为 activeElement 没有及时更新以供代码执行。所以我们稍等一下。
如果您使用 Tab 键进入 iframe,这也会触发。对于我的用例来说,这很好,但您可以过滤该按键。
My approach was similar to that proposed by Paul Draper above. However, it didn't work in Firefox because activeElement did not update in time for the code to execute. So we wait a little bit.
This will also fire if you tab into the iframe. For my use case, it's fine, but you could filter for that keypress.
我相信你可以这样做:
使用 jQuery 来完成这个任务。
I believe you can do something like:
using jQuery to accomplish this.
以下代码将显示用户是否单击/悬停或移出 iframe:-
您需要将 iframe 中的 src 替换为您自己的链接。希望这会有所帮助。
问候,
莫。
The following code will show you if the user click/hover or move out of the iframe:-
You need to replace the src in the iframe with your own link. Hope this'll help.
Regards,
Mo.
刚刚找到这个解决方案...
我尝试过,我喜欢它..
适用于桌面和移动设备的跨域 iframe!
不知道是否万无一失
快乐编码
Just found this solution...
I tried it, I loved it..
Works for cross domain iframes for desktop and mobile!
Don't know if it is foolproof yet
Happy coding
您可以通过使用窗口元素上的模糊事件来实现此目的。
这是一个用于跟踪 iframe 上点击的 jQuery 插件(当点击 iframe 时它将触发自定义回调函数):
https://github.com/finalclap/iframeTracker-jquery
像这样使用它:
You can achieve this by using the blur event on window element.
Here is a jQuery plugin for tracking click on iframes (it will fire a custom callback function when an iframe is clicked) :
https://github.com/finalclap/iframeTracker-jquery
Use it like this :
请参阅 http://jsfiddle.net/Lcy797h2/ 了解我的冗长解决方案,该解决方案在IE
see http://jsfiddle.net/Lcy797h2/ for my long winded solution that doesn't work reliably in IE
http://jsfiddle.net/QcAee/406/
只需在 iframe 上创建一个不可见的层即可单击时返回,并在 mouseleave 事件被触发时向上!!
需要 jQuery,
此解决方案不会在 iframe 内传播第一次点击!
http://jsfiddle.net/QcAee/406/
Just make a invisible layer over the iframe that go back when click and go up when mouseleave event will be fired !!
Need jQuery
this solution don't propagate first click inside iframe!
这适用于所有浏览器(包括 Firefox)
https://gist.github.com/jaydson/1780598
https://jsfiddle.net/sidanmor/v6m9exsw/
This works for me on all browsers (included Firefox)
https://gist.github.com/jaydson/1780598
https://jsfiddle.net/sidanmor/v6m9exsw/
<iframe id="idanmorblog" src="https://sidanmor.com/" style="width:400px;height:600px" ></iframe>
将上述答案与一次又一次单击的能力相结合,而无需单击外部 iframe。
Combining above answer with ability to click again and again without clicking outside iframe.
穆罕默德·拉德万,
你的解决方案很优雅。要检测 Firefox 和 IE 中的 iframe 点击,您可以使用带有 document.activeElement 和计时器的简单方法,但是...我在互联网上搜索了一种检测 Chrome 和 Safari 中 iframe 点击的方法。在放弃的边缘,我找到了你的答案。谢谢您,先生!
一些提示:
我发现直接调用 init() 函数(而不是通过 AttachOnloadEvent())时您的解决方案更可靠。当然,要做到这一点,您必须仅在 iframe html 之后调用 init() 。所以它看起来像:
Mohammed Radwan,
Your solution is elegant. To detect iframe clicks in Firefox and IE, you can use a simple method with document.activeElement and a timer, however... I have searched all over the interwebs for a method to detect clicks on an iframe in Chrome and Safari. At the brink of giving up, I find your answer. Thank you, sir!
Some tips:
I have found your solution to be more reliable when calling the init() function directly, rather than through attachOnloadEvent(). Of course to do that, you must call init() only after the iframe html. So it would look something like:
您可以执行此操作,将事件冒泡到父文档:
只需扩展事件列表以获取更多事件。
You can do this to bubble events to parent document:
Just extend the eventlist for more events.
我遇到了一种情况,我必须跟踪通过 iframe 拉入的社交媒体按钮的点击次数。单击按钮时将打开一个新窗口。这是我的解决方案:
I ran into a situation where I had to track clicks on a social media button pulled in through an iframe. A new window would be opened when the button was clicked. Here was my solution:
如果 iframe 与您的父站点来自同一域,这绝对有效。我还没有测试跨域站点。
如果没有 jQuery,你可以尝试这样的方法,但我还没有尝试过。
您甚至可以过滤结果:
This definitely works if the iframe is from the same domain as your parent site. I have not tested it for cross-domain sites.
Without jQuery you could try something like this, but again I have not tried this.
You can even filter your results:
我们可以捕获所有的点击。这个想法是在每次点击后重置 iFrame 外部元素的焦点:
JSFiddle
We can catch all the clicks. The idea is to reset focus on an element outside the iFrame after each click:
JSFiddle
假设 -
这适用于有源和无源 iframe
如果打开新选项卡/卸载同一页面并且鼠标指针位于 iframe 内,则视为单击
Assumptions -
This works for both sourceful and sourceless iframes
If a new tab is opened / same page unloads and the mouse pointer is within the Iframe, a click is considered
根据 Paul Draper 的回答,我创建了一个解决方案,当您拥有在浏览器中打开其他选项卡的 Iframe 时,该解决方案可以连续工作。当你返回页面继续处于活动状态时检测框架上的点击,这是一种很常见的情况:
代码很简单,blur 事件检测 iframe 被点击时失去焦点,并测试活动元素是否为iframe(如果您有多个 iframe,您可以知道谁被选中)当您有宣传框架时,这种情况经常发生。
当您返回页面时,第二个事件会触发 focus 方法。它用于可见性更改事件。
Based in the answer of Paul Draper, I created a solution that work continuously when you have Iframes that open other tab in the browser. When you return the page continue to be active to detect the click over the framework, this is a very common situation:
The Code is simple, the blur event detect the lost of focus when the iframe is clicked, and test if the active element is the iframe (if you have several iframe you can know who was selected) this situation is frequently when you have publicity frames.
The second event trigger a focus method when you return to the page. it is used the visibility change event.
这是使用悬停+模糊和活动元素技巧的建议方法的解决方案,没有任何库,只是纯js。适用于 FF/Chrome。大多数方法与 @Mohammed Radwan 提出的方法相同,只是我使用 @zone117x 提出的不同方法来跟踪 FF 的 iframe 点击,因为如果没有添加 window.focus 就无法工作。 //developer.mozilla.org/en-US/docs/Web/API/Window/focus" rel="nofollow noreferrer">用户设置:
这是复合方法:
Here is solution using suggested approaches with hover+blur and active element tricks, not any libraries, just pure js. Works fine for FF/Chrome. Mostly approache is same as @Mohammed Radwan proposed, except that I use different method proposed by @zone117x to track iframe click for FF, because window.focus is not working without addition user settings:
Here is compound method:
这当然是可能的。这适用于 Chrome、Firefox 和 IE 11(可能还有其他版本)。
警告:这仅检测第一次点击。据我了解,这就是你想要的。
This is certainly possible. This works in Chrome, Firefox, and IE 11 (and probably others).
Caveat: This only detects the first click. As I understand, that is all you want.
这是一个适用于所有浏览器甚至 IE8 的小解决方案:
您可以在这里测试它:http://jsfiddle.net/oqjgzsm0/
This is small solution that works in all browsers even IE8:
You can test it here: http://jsfiddle.net/oqjgzsm0/
根据 Mohammed Radwan 的回答,我想出了以下 jQuery 解决方案。基本上它的作用是跟踪 iFrame 人们悬停的内容。然后,如果窗口模糊,则很可能意味着用户单击了 iframe 横幅。
iframe 应该放在带有 id 的 div 中,以确保您知道用户单击了哪个 iframe:
所以:
...
当没有 iFrame 悬停时,这会将 overiFrame 保持为 -1,或者当 iframe 悬停时,在包装 div 中设置“bannerid”。您所要做的就是检查窗口模糊时是否设置了“overiFrame”,如下所示:
...
非常优雅的解决方案,但有一个小缺点:如果用户在将鼠标悬停在 iFrame 上时按下 ALT-F4,它会将其记录为单击。不过这只发生在 FireFox 中,IE、Chrome 和 Safari 没有注册它。
再次感谢穆罕默德,非常有用的解决方案!
Based on Mohammed Radwan's answer I came up with the following jQuery solution. Basically what it does is keep track of what iFrame people are hovering. Then if the window blurs that most likely means the user clicked the iframe banner.
the iframe should be put in a div with an id, to make sure you know which iframe the user clicked on:
so:
...
this keeps overiFrame at -1 when no iFrames are hovered, or the 'bannerid' set in the wrapping div when an iframe is hovered. All you have to do is check if 'overiFrame' is set when the window blurs, like so:
...
Very elegant solution with a minor downside: if a user presses ALT-F4 when hovering the mouse over an iFrame it will log it as a click. This only happened in FireFox though, IE, Chrome and Safari didn't register it.
Thanks again Mohammed, very useful solution!
不。您所能做的就是检测鼠标进入 iframe,并可能(尽管不可靠)检测鼠标何时返回(即尝试找出在其他地方经过广告的指针与停留在广告上的指针之间的区别)在广告上)。
不,没有办法伪造点击事件。
通过捕获 mousedown,您可以防止原始点击到达 iframe。如果您可以确定鼠标按钮何时将被按下,您可以尝试将不可见的 div 移开,以便单击能够通过...但是在鼠标按下之前也不会触发任何事件。
您可以尝试猜测,例如查看指针是否静止,猜测可能即将发出咔嗒声。但这是完全不可靠的,如果你失败了,你就失去了点击率。
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).
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.