📅  最后修改于: 2023-12-03 15:16:10.443000             🧑  作者: Mango
Javascript 刷新警报是一个用于提醒用户刷新网页的功能,通常在网页被更新但用户并未刷新时使用。本文将介绍如何使用Javascript实现此功能。
<div id="refresh-alert" style="display: none;">网页已更新,请刷新。</div>
window.addEventListener('load', function() {
window.addEventListener('unload', function() {
localStorage.clear();
});
});
window.addEventListener('storage', function(event) {
if (event.key === 'clear-cache') {
document.getElementById('refresh-alert').style.display = 'block';
}
});
function refreshPage() {
document.getElementById('refresh-alert').style.display = 'none';
location.reload();
}
<!DOCTYPE html>
<html>
<head>
<title>Javascript 刷新警报</title>
</head>
<body>
<div id="refresh-alert" style="display: none;">网页已更新,请刷新。</div>
<button onClick="refreshPage()">刷新</button>
<script>
window.addEventListener('load', function() {
window.addEventListener('unload', function() {
localStorage.clear();
});
});
window.addEventListener('storage', function(event) {
if (event.key === 'clear-cache') {
document.getElementById('refresh-alert').style.display = 'block';
}
});
function refreshPage() {
document.getElementById('refresh-alert').style.display = 'none';
location.reload();
}
</script>
</body>
</html>