views:

21

answers:

1

Is it possible to programmatically fire mouse events in DOM? My sample case would be the following:

<html>
  <body>
    <iframe style="width: 500px; height: 500px;" src="something.html"></iframe>
    <div id="layer" style="position: absolute; left: 0px; top=0px; width=500px; height=500px;"></div>
  </body>
</html>

Whenever the user clicks the div over an iframe, I would like to somehow propagate the event to the iframe, too. (Here we assume that the iframe src is in the same domain.)

+2  A: 

Whilst you can inject events into browsers' event-handling systems (in not entirely portable ways), it will only cause event handlers registered on those events to be called. It won't cause default actions like following clicked links. Also you don't get the target element worked out for you from co-ordinates so you'd have to do that yourself.

A better bet would be to lose the obscuring <div> and register a click handler on the iframe's document, which then informs code in the parent of the location of the click.

bobince
...unless the iframe *src* is a different domain, in which case you're out of luck :-)
Andy E
Added a note about being in the same domain.
Eemeli Kantola
Alright, so the best solution was to register an onclick handler for the iframe content's `document.body`. Thanks for the tip!
Eemeli Kantola

related questions