Answer the question
In order to leave comments, you need to log in
How to check if the user is on an open tab?
I can not check whether the user is on a new open tab in any way?
I do so
newWindow = window.open('https://google.com', '_blank', 'status=0,menubar=0,toolbar=0,resizable=1,scrollbars=1,height=' + 750 + ',width=' + 1000 + ',left=' + (screen.width / 2 - 1000 / 2) + ',top=' + (screen.height / 2 - 750 / 2);
newWindow.onblur = function() { hasFocus = false; console.log('focus: false'); };
newWindow.onfocus = function() {
hasFocus = true;
console.log('focus: true');
};
Answer the question
In order to leave comments, you need to log in
There is a Page Visibility API (in English).
From there, an example of code that pauses video playback if the tab becomes inactive:
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
var videoElement = document.getElementById("videoElement");
// If the page is hidden, pause the video;
// if the page is shown, play the video
function handleVisibilityChange() {
if (document[hidden]) {
videoElement.pause();
} else {
videoElement.play();
}
}
// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || hidden === undefined) {
console.log("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
// Handle page visibility change
document.addEventListener(visibilityChange, handleVisibilityChange, false);
// When the video pauses, set the title.
// This shows the paused
videoElement.addEventListener("pause", function(){
document.title = 'Paused';
}, false);
// When the video plays, set the title.
videoElement.addEventListener("play", function(){
document.title = 'Playing';
}, false);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question