背景
Web应用程序中,在列表页面选中并点击进入子页面处理事务,完成之后返回上一页并刷新列表是非常常规的操作。在Android环境中非常正常,然而在iOS环境中返回上一页之后并不能自动刷新页面。
寻找原因
其实返回的代码也只是常规的window.history.back()
,问题还是在与浏览器方面,简单在网上搜一下就可以搜到很多的解决方法了。
解决
导致该原因是因为iOS的缓存机制问题,iOS对返回上一页的操作往往只会读取缓存中的页面,所以在返回到上一页的时候需要强制刷新当前页面:
solution1.html
<!-- monitor the onpageshow event and set reload -->
<script type="javascript">
window.onpageshow = function(event) {
if (event.persisted) {
window.location.reload()
}
};
</script>
solution2.html
<!-- create a new iframe to force the page different -->
<script type="javascript">
var $iframe = $('<iframe src="/favicon.ico"></iframe>').on('load', function() {
setTimeout(function() {
$iframe.off('load').remove()
}, 0)
}).appendTo($('body'));
</script>
参考
Prevent safari loading from cache when back button is clicked
Is *onbeforeunload* cached on Safari (macOS)?