5

Currently in my page I am using this to refresh the page each minute:

<head>
...
<meta http-equiv="refresh" content="60" />
...
</head>

How instead, I want to refresh the page when a user presses the tab. Lets say a user has multiple tabs open in the web browser. And he uses a few minutes surfing on another tab. When he then press the tab for my website, I want the page to autorefresh, so the user don't have to do it himself to check for any updates on the page. How can I accomplish this?

7
  • stackoverflow.com/questions/20783015/… Commented Nov 3, 2020 at 8:57
  • Note that when it's implemented the users experience can be rather unexpected and hence misleading. He had been on your site and after coming back it disappears, is empty for a while and hopefully loads again.
    – wiktus239
    Commented Nov 3, 2020 at 8:57
  • you could put a javascript function onto your body tag with the onclick parameter then have the js funtion reload the page Commented Nov 3, 2020 at 8:59
  • @sergeykuznetsov I don't want to refresh when pressing a button or link on my page, but I want my page to refresh when the user presses the tab for my website (tabs on the top of the web browser).
    – Anefi3
    Commented Nov 3, 2020 at 9:09
  • 1
    Use @Andrea Viviani's answer. That's what you need. Commented Nov 3, 2020 at 9:29

1 Answer 1

9

you should use js and visibility API:

include this small script in your html page:

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

Adapted from here: Event for when user switches browser tabs

0

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