I am making website with contests and I want to check, if user's tab in browser is active and visible. For example, if person hides browser window or switches tab with my website, the tab won't be active and I need to know it. Can i do this or something like this?
I know about Firefox visibility API, but it checks only visibility. So, if there are two windows on the screen and one of them is browser window with website, website is visible. But other window can be active and my website will be visible, but not active. It It isn't something that I want
try:
document.addEventListener("visibilitychange", function() {
console.log( document.hidden );
});
window.addEventListener('focus', function() {
console.log("window is active!" );
});
window.addEventListener('blur', function() {
console.warn("window is not active!" );
});
Ok, it seems that I need to use document.hasFocus() (docs) js function. It returns false if tab isn't visible or other window is active, else it returns true
Related
I am creating an online exam platform, so I don't want the user to navigate and close the browser. I have used an onblur event and visibility api. Both have limitations.
For example, onblur triggers even when the window is open and the user clicks on empty space in toolbar of the computer. The visibility api only works if the browser window is minimized. It's not applicable if it is overlapped by another window.
$(window).blur(function() {
document.addEventListener("visibilitychange", function() {
if (document.hidden) {
alert('blur');
} else {
console.log("Browser tab is visible")
}
});
})
I have a messenger on PHP/js, imported to all pages of the website. The messenger works through ajax, checking for new messages each 5 seconds.
Also I have desktop and sound notifications.
The problem is, if I opened multiple pages, and I'm currently on one of them, the reminder may come from another page, which is currently not active. However, inactive pages should notify only if the active page is not the same website.
Ideas?
Check for window focus:
var window_focus;
$(window).focus(function() {
window_focus = true;
}).blur(function() {
window_focus = false;
});
Check if location.host matches specified domain and window is focused:
if((location.host == "example.com")&&(window_focus == true))
{
//window is focused and on specified domain for that website.
//sounds, notifications, etc. here
}
Not completely sure if this is what you mean, but I hope it helps.
I want my javascript to be trigged when:
The current IE tab is switched out when multiple IE tabs are open.
When the current IE tab is closed.
I don't want my JS code be trigged by the in-page pops up dialogs.
When the whole IE window closed.
The lose focus event may not work for me because there are pop up diaglogs in my page, so when it pops out, the IE tab will lose focus, but since the tab is not switched or closed, I don't want my javascript to be trigged here.
Is there any solution? I am wondering if there's something like entering tab / leaving tab, or tab-switching events?
Some interesting links, but not resolve my question.
Is there a way to detect if a browser window is not currently active?
Hook into tab changed event of browser
if you use 'jQuery', you can easily do it .
$(window).blur(function(){
// your code
});
$(window).focus(function(){
// your code
});
here is the link which provides one more method to do it.
you may be interested in
(function(){
function doOnFocus(){ console.log("focus"); }
function doOnBlur(){ console.log("blur"); }
function doOnLeave(){ console.log("leave"); }
if('onfocusout' in document){
document.onfocusout = doOnBlur;
document.onfocusin = doOnFocus;
}else{
window.onblur = doOnBlur;
window.onfocus = doOnFocus;
}
window.onbeforeunload = doOnLeave;
})();
In javascript there is an event on window close it is not IE specific but is mostly used to call an alert before the user leaves the page. It's one of my pet peeves and is very annoying but may be what you are looking for.
window.onbeforeunload = yourfunctionthatexecutes;
My extension opens up a series of additional windows. I want those windows to close when the user closes the main Firefox window. I know you can detect when a tab closes (and perhaps I should just look for the "final" tab close?) but I want to know when "all tabs" have been closed.
In short, how can I detect when the main Firefox window is closed from an extension?
You can listen the unload event.
If you script run on all type of windows, then check the url whether a browser window.
window.addEventListener('unload', function(event) {
if (event.target.location.href !== 'chrome://browser/content/browser.xul') {
return;
}
// do you stuff...
}, false);
I found one solution, although it doesn't actually listen for a particular event. Instead, we setup an interval to check if the tab container is still available.
setInterval(function() {
if (typeof gBrowser.mTabContainer === 'undefined') {
// Rest of your code...
}
}, 500);
If there's a cleaner way of doing this, I'd be more than happy to see it.
This question already has answers here:
Is there a way to detect if a browser window is not currently active?
(24 answers)
Closed 8 years ago.
I wish to determine using jquery or javascript that is the user has switched tab standing on the same browser?
I.e. if the user is currently standing on browser say mozilla-firefox tab no.1 , now he has opened another tab say tab no.2 , at that point a popup should appear , with the message "tab changed"
I believe you can use the window.onblur event which will fire when the current page loses focus.
window.onblur = function() {
// Your action here
};
In jQuery, you write it this way
$(window).blur(function() {
// Your action here
});
Without jQuery
window.onblur = function () {
// do some stuff after tab was changed e.g.
alert('You switched the tab');
}
with jQuery:
$('window').blur(function () {
// do some stuff after tab was changed e.g.
alert('You switched the tab');
});
Of course displaying alert is not best thing to do, because it focus current tab again :)
this might work, but will popup when the user leaves th esite in anyway not just tab changes
window.onunload = popup;
function popup() {
alert('tab changed');
}