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.
2
Answers
Safari on iOS has unique behavior when it comes to managing history and caching pages. The best method is to use the
pageshow
event, which is triggered when a page is navigated back to, andpersisted
event which tells if a webpage is loading from a cache or not.When you call window.location.reload() and then window.history.back(), Safari first reloads the page and then navigates back to the previous page. This causes the page to be reloaded twice, which is why it doesn’t update with the new data.
You can use the window.history.go(-1) method instead of window.history.back(). This method navigates the browser to the previous page in the history, without reloading the current page.