0

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);

0

1 Answer 1

0
  1. for unload tab or tab close , then prompt

    window.addEventListener('beforeunload', function (e) { alert("The window is closing now!"); });

  2. 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") } });

Not the answer you're looking for? Browse other questions tagged or ask your own question.