我想知道是否可以执行此操作:
我有一个网站(基于Elementor构建的Wordpress)。我会定期在网站上添加一些内容,一旦在页面上添加/更改内容,我是否可以强制所有正在浏览该页面的用户以某种方式刷新?
问题可能不是stackoverlow的问题,而是因为我无法找到所需的内容而寻求帮助。
答案 0 :(得分:0)
答案 1 :(得分:0)
强制重载是用户可能会讨厌的东西。我想建议不同的方法:创建JavaScript / jQuery代码以定期检查例如。如果存在某些文件。要重新加载页面后,只需创建文件即可。
应该扩展该示例,以检查用户是否已经重新加载页面,以避免再次显示通知栏,即使该文件仍然存在。您可以为此目的使用cookie。
下面的代码只是为了演示这种想法,它不是一个复杂的解决方案。
jQuery(function($) {
//check on load - shloud be removed
checkFileExists();
//call function every 5 minutes
setInterval(function() {
checkFileExists();
}, 1000 * 60 * 5);
//check if a given file exists
function checkFileExists() {
var http = new XMLHttpRequest();
http.open('HEAD', "https://file-examples-com.github.io/uploads/2017/02/file_example_JSON_1kb.json", false);
http.send();
if (http.status === 200) {
$("#notice-bar").show();
//you can reload the page here, just uncomment the line below
//location.reload(true);
$("#notice-bar").show();
} else {
//File doesn't exists
$("#notice-bar").hide();
}
}
//reload page once the link is clicked
$('#reload').click(function() {
location.reload(true);
});
});
#notice-bar {
position: absolute;
display: none;
top: 0;
width: 100%;
height: auto;
text-align: center;
padding: 0.5em;
background-color: #FFDDA9;
color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="notice-bar">
<p>New version available. Please <a href="#" id="reload">reload the page</a>.</p>
</div>
</body>
</html>