Search code examples
javascriptiosresponsive

Page reload in IOS devices using javascript in


The screens in my IOS device doesn't reload after clicking the back button. It works using PC and android devices. Is there specific reload code for IOS device?

here is my code

```
<script>
  document.addEventListener("DOMContentLoaded", function() {
    var goBackButton = document.getElementById("goBackButton");

    if (goBackButton) {
      goBackButton.addEventListener("click", function() {
        window.location.reload();
        window.history.back();
     });

      goBackButton.addEventListener("touchstart", function() {
        window.location.href = window.location.href
        window.history.back();
      });
    }
  });
</script>
```

I was expecting it to reload the screen so it updates the newly fetched data after clicking the back button.


Solution

  • If you want to navigate to the previous page without using window.history.back(), you can achieve this by accessing the document.referrer property, which contains the URL of the previous page.

    Here's how you can modify your script to achieve this:

    <script>
      document.addEventListener("DOMContentLoaded", function() {
        var goBackButton = document.getElementById("goBackButton");
    
        if (goBackButton) {
          goBackButton.addEventListener("click", function() {
            window.location.href = document.referrer;
          });
    
          // Handle touch events for mobile devices
          goBackButton.addEventListener("touchstart", function() {
            window.location.href = document.referrer;
          });
        }
      });
    </script>