Intercept click on content in an iframe

不羁的心 提交于 2020-01-15 10:08:06

问题


I have an iframe that references an external URL that serves up pages that contain Flash adverts.

I need to track how often a customer clicks on one of those adverts.

The approach I am taking is to render a div element over the iframe. This allows me to intercept the click event, however I need to pass that click down to the iframe. Is this possible using JavaScript?


回答1:


No, it's not possible. You can't simulate a real click in javascript, you can only fire click events.




回答2:


I do not think that it is possible as well

But, assuming that the clicks redirect the user to the site of the advert, you could intercept the user click using redirections. Change the link to some script on your own server with some unique advert id. Register the click and redirect the user to the advertisement page.

Another possibility is to use this technique to load the contents of the iframe, so you known the amount of customers viewed the advertisement. But this of course might be an advertisement scheme your advertisement customer does not like/want.




回答3:


You can't pass the click through by any legitimate means, and you'll run up against cross-domain problems if you tried to fake it in anyway. And I would definitely stay away from anything that looks like a clickjacking solution - it's bound to stop working (and feels evil too).

You may be able to hack something, depending on how accurate it has to be. This would involve tracking the sequence of events happening when user has put their mouse into the banner area and then left the page (inferring that they clicked on the ad). You'll miss some, and you may catch some false positives too.

It would work something like:

  1. Leave the covering div in place
  2. onMouseOver, hide the div and set an onbeforeunload event handler that registers a "click" through an AJAX post (or similar)
  3. when the mouse moves out of the banner area it means they didn't click the ad, so show the div again and remove the event handler

I'd guesstimate you'd get about 80-90% accuracy, but you're going to have to test on a lot of browsers. It's also assuming the ad loads into the same window and not a new one. If it loads into a new one then I think it's going to be even harder.



来源:https://stackoverflow.com/questions/2085752/intercept-click-on-content-in-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!