Iframe onmouseout capture

I currently have an iframe on my parent page, and I was wondering if it is possible to capture the onmouseout event to intercept when the user clicks or goes outside the iframe, i.e. back to the parent page.

+4
source share
1 answer

You should be able to do document.body.onmouseleave / onmouseout (see edits) on the iframe page. Since these are bubbles, you will need to verify that this is actually the body that triggered the event.

document.body.onmouseout = function () { if (event.srcElement == document.body) { // put your code here } } 

EDIT : my mistake is that onmouseleave does not bubble, so if statement is not required. onmouseleave does not work for other browsers, so you should use onmouseout and save the if statement because onmouse does not work.

if only IE6 bothers you, put the following <script> tag in your iframe page code (preferably a header).

 <script type="text/javascript"> document.documentElement.onmouseleave = function () { alert('mouse left the iframe'); } </script> 

See MSDN documentation here

+3
source

All Articles