i'm working on a small sale website. i want to use visibilitystate to dictate when the user navigates to a new page, switches tabs, closes the tab, minimizes or closes the browser, or, on mobile, switches from the browser to a different app. to return a new URL. document.addEventListener("visibilitychange", () => {
if (document.visibilityState === 'hidden') {
setTimeout(function(){
window.location.href = 'www.google.com';
}, 1000);
Add a comment
|
1 Answer
for unload tab or tab close , then prompt
window.addEventListener('beforeunload', function (e) { alert("The window is closing now!"); });
if want to detect tab switch then,
document.addEventListener("visibilitychange", (event) => { if (document.visibilityState == "visible") { console.log("tab is active") } else { console.log("tab is inactive") } });