1

I have seen this used on a few websites, but can't find the answer.

Let's say you open up a website_A.com and you DON'T close it.

then you go to another tab (website_B.com). You browse there for a while...

Then you click back on website_A.com tab and the website automatically refreshes.

I know there's:

<meta http-equiv="refresh" content="180">

but it refreshes a website after a fixed amount of time.

How to refresh when the browser detects that you are back browsing the website?

Need help.

robert0
  • 435
  • 2
  • 12

1 Answers1

2

Found a solution (Thanks to Manas Khandelwal).

Just put this anywhere on your site:

<script>
document.addEventListener("visibilitychange", function() {
   if (document.hidden){
       console.log("Browser tab is hidden")
   } else {
       console.log("Browser tab is visible")
       location.reload();
   }
});
</script>
robert0
  • 435
  • 2
  • 12