Here is a solution that asynchronously reloads a page using jQuery. It avoids the flicker caused by window.location = window.location
. This example shows a page that reloads continuously, as in a dashboard. It is battle-tested and is running on an information display TV in Times Square.
<!DOCTYPE html>
<html lang="en">
<head>
...
<meta http-equiv="refresh" content="300">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script>
function refresh() {
$.ajax({
url: "",
dataType: "text",
success: function(html) {
$('#fu').replaceWith($.parseHTML(html));
setTimeout(refresh,2000);
}
});
}
refresh();
</script>
</head>
<body>
<div id="fu">
...
</div>
</body>
</html>
Notes:
$.ajax
directly like $.get('',function(data){$(document.body).html(data)})
causes css/js files to get cache-busted, even if you use cache: true
, that's why we use parseHTML
parseHTML
will NOT find a body
tag so your whole body needs to go in an extra div, I hope this nugget of knowledge helps you one day, you can guess how we chose the id for that div
http-equiv="refresh"
just in case something goes wrong with javascript/server hiccup, then the page will STILL reload without you getting a phone callhttp-equiv
refresh fixes that