Is there any javascript library
to detect whether the tab is in focus or not and then execute the code accordingly?
Asked
Active
Viewed 712 times
-3

Daksh Shah
- 2,997
- 6
- 37
- 71

Er.Rahul Kr
- 17
- 2
-
I think you can do it with native JS, something like `window.onfocus = function() { console.log('focussed'); }` – putvande Apr 30 '14 at 12:28
-
For sure there is a jQuery library for this. – hsz Apr 30 '14 at 12:29
2 Answers
4
By this code you can check whether the window is in focus or not:
window.addEventListener('focus', function() {
document.title = 'focused';
});
window.addEventListener('blur', function() {
document.title = 'not focused';
});
And other thing, you have to code it up yourself if you face problems ask a question and add the code you tried along with it
Update: There is a little problem with this and also with the other answer given, the problem is that just scrolling won't make it work. You will need to interact with the page to make it work. But the accepted answer given here might work for scrolling too

Community
- 1
- 1

Daksh Shah
- 2,997
- 6
- 37
- 71
2
You can possible do this with native JS code like:
window.onfocus = function() {
console.log('focussed');
}
window.onblur = function() {
console.log('blurred');
}

putvande
- 15,068
- 3
- 34
- 50