Purpose: to keep the page from being cached and automatically refreshed.
problem
The front end has made a timer to get data from the back end in real time, and the data is stuffed into the jsp at the back end.
After I returned to this page from another page, the page fetched data from the cache, which caused a timing error.
solve
1. Add meta tag setting to not use cache
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
2. Add refresh operation
window.onpageshow = function(event) {
if (event.persisted || window.performance && window.performance.navigation.type == 2) {//页面是从缓存中获取的数据||是通过浏览器后退来到该页面
window.location.reload();
}
}
- window.performance.navigation.type == 2 Return to this page by going back
- event.persisted is true when getting data from the cache, otherwise it is false
expand
window.performance
-
The navigation field counts some data related to web page navigation:
-
redirectCount : the number of redirects (read-only), but this interface has the same-origin policy restriction, that is, only the same-origin redirects can be detected;
-
The return value of type should be one of 0,1,2. Corresponding to three enumeration values:
- 0: TYPE_NAVIGATE (Users access the page through conventional navigation methods, such as clicking a link, or general get method)
- 1: TYPE_RELOAD (Users access the page by refreshing, including JS calling the refresh interface, etc.)
- 2: TYPE_BACK_FORWARD (Users access this page through the back button)
Link to this article: https://blog.csdn.net/qq_39903567/article/details/114981256