Reputation: 4127
my goal is to auto-refresh some components with jquery when the "page tab" is focussed. I have a "sort of cart" and i want to auto-update it even if you are displaying my site from more than 1 tab. Also, i'm trying to make it work from an iframe inside a page!
is this possible or i must use some timer to auto-refresh periodically?
edit: after reading the solution i made it work once for every focus, don't know if it's the best solution but it gets the job done :D
var focusPage = false;
var firstFocus = false;
$(window).focus(function () {
focusPage = true;
if (firstFocus) {
firstFocus = false;
//BAM! Refresh your stuff
}
});
$(window).blur(function () {
focusPage = false;
firstFocus = true;
});
Upvotes: 3
Views: 514
Reputation: 11
Just what you have written above can be achieved by this
window.onblur= function() {
window.onfocus= function () { location.reload(true); }
};
It will do things just once. You can reduce the code by this call.
Upvotes: 1
Reputation: 5291
You can catch focus
event on window.
Try to use:
$(window).focus(function(){
//refresh your div;
});
Upvotes: 4
Reputation: 20431
Using this seems to work well:
$(document).ready(function () {
$(window).focus(function () {
console.log("bam!");
})
});
Upvotes: 4