I have a main page in which I have an iframe
I have bound a function to be executed whenever the main page gets focus (that will
be after the user, opens another tab or window and then comes back to the page)
When the user clicks on a button inside the main page, an iframe opens, and if that iframe is posted back or reloads, then the focus event of the main page stops working.
In the following code, I have tried an alternate solution, by rebinding the focus event to the main page’s window whenever the contents of the iframe are reloaded. But, still it is not able to bind the focus event again to the main page’s window.
Can somebody please help ??
$(document).ready(function() {
$(window).focus(function() {
CheckChange();
});
$("iframe").load(function() {
alert('1');
alert(top.window);
$(top.window).focus(function() {
alert('2');
CheckChange();
});
});
});
I got its answer, after I open the iframe in the main page, and then switch between the browser tabs, the item which gets focus is the iframe itself and the parent window never gets focus, that’s why its focus event is not called. What gets called is the focus event of the iframe, which I tried very hard to attach to iframe but, eventually I found out that with jquery, the focus event handler was not getting attached to iframe, I declared everywhere in my html pages on each iframe element explicitly the
"onfocus=eventHandler"attribute and then the iframe started getting focus while switching between the browser tabs.