Catch all events in iframe jQuery and replicate

风流意气都作罢 提交于 2019-12-23 02:41:55

问题


this is the question:

I have an iframe that displays any web page (not necessarily developed by me). On this page may be attached events (which presumably I do not know).

how can I dynamically intercepting these events and store them for later replicate?

for example: suppose that when the mouse moves over a certain div it changes color. I would like to make sure that when you trigger the event that would change color to the div it is "registered" with all A series of information that will allow me (without user interaction) to replicate it at a later time.

ideas? for both the recording and subsequent replication


回答1:


The short answer is: you cannot.

You cannot access the document object of "any web page" with JavaScript on your page if it is on a different domain, which I assume if you say "any web page", because of the Same Origin Policy. You would need the website in the IFRAME to cooperate with your script to achieve this, which will not happen with "any web page".

IFRAME on same domain
If your web page is on the same domain then you can access the events of the IFRAMEs body element by adding a listener for every event you want to catch like described here. This is possible for all events that bubble up to the body. So if you have Events that are prevented from bubbling upwards to the body, they will not be catched with this solution.

jQuery('#website body').on('click mouseover mouseout', function(event) {
    console.log(event.type);
});

Assuming you have an IFRAME with the id website you can catch the events you wish by listing them separated with spaces as above. The example catches click, mousover and mouseout.

Maybe this is closer to what you want?




回答2:


Add event handlers to your divs. For your example you could use

$('#div').mouseover(function(e) { ... }) 

or

('#div').on('mouseover', function(e) { ... })

For 'replication', you'd have to store information about past events in some object. You could even store the event object itself.



来源:https://stackoverflow.com/questions/13780667/catch-all-events-in-iframe-jquery-and-replicate

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