tags:

views:

292

answers:

1

Hi,

I currently have an iframe within my parent page and I was wondering whether it's possible to capture a onmouseout event to trap when the user clicks or moves outside the iframe border, i.e back onto parent page.

Would really appreciate some help.

Thanks. Tony.

+2  A: 

You should be able do a document.body.onmouseleave/onmouseout (see edits) in the iframe's page. Since it bubbles you'll have to check that it was actually the body that fired the event.

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

EDIT: My mistake, onmouseleave doesn't bubble, so the if statement there isn't necessary. onmouseleave doesn't work for other browsers, so you should use onmouseout and keep the if statement in since onmouseout does bubble.

if you're only bothered about IE6, put the following <script> tag into your iframe page's code (preferably the header).

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

See the MSDN documentation here

Andy E
thanks andy - fyi, I am using IE6. sorry, I am still a bit unsure of what I need to do within the iframe page?
tonsils
tried placing your code within iframe page and received the error: 'document.body' is an null or not an object?
tonsils
That's because the script is initializing before the body has been processed. Adding the defer attribute to the script will make it run after the document has fully parsed. Also, adding the event to the documentElement (HTML tag) should work just as well, with no need for the defer attribute in the script.
Andy E
All good Andy - thanks mate.
tonsils
No worries, glad it's sorted
Andy E