leaving iframe event

pamela rizk
pamela rizk used Ask the Experts™
on
is there any way to catch event when leaving  iframe ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Steven KribbeSoftware Engineer

Commented:
You can detect the loss of the focus of an iframe with this small piece of jQuerycode:

$(document).focusout( function(){
       setTimeout(function() {
       // used setTimeout to make sure the event is passing the event loop and call your logic as well
       if (document.activeElement instanceof HTMLIFrameElement) {
             // do stuff
        }
    },0);
});
Developer
Commented:
it has been solved using the below method:

 var eventMethod = window.addEventListener
                        ? "addEventListener"
                        : "attachEvent";
            var eventer = window[eventMethod];
            var messageEvent = eventMethod === "attachEvent"
                ? "onmessage"
                : "message";
            eventer(messageEvent, function (e) {
               
               alert('Message from iframe just came!');
            });
pamela rizkDeveloper

Author

Commented:
this solution worked!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial