Skip to content

Instantly share code, notes, and snippets.

@immortalsantee
Forked from petenelson/api.php
Created January 25, 2014 12:05
Show Gist options
  • Save immortalsantee/8615359 to your computer and use it in GitHub Desktop.
Save immortalsantee/8615359 to your computer and use it in GitHub Desktop.
Force all browsers to reload on any attached browser's page reload
<?php
// just using a file, could easily be replaced by some other storage method
if (isset($_REQUEST['new']) && $_REQUEST['new'] == '1') {
$ver = new stdClass();
$ver->ver = time();
file_put_contents('ver.json', json_encode($ver));
}
else {
echo file_get_contents('ver.json');
die();
}
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<meta name="viewport" content="initial-scale=1" />
</head>
<body>
<div id="ver" style="font-size: 4em;"></div>
<img src="http://baconmockup.com/300/300/bacon" />
<script type="text/javascript">
var version = '';
jQuery(document).ready(function() {
setTimeout(reloadOnNewVersion, 250);
});
window.onbeforeunload = function(e) {
setNewVersion();
}
function setNewVersion() {
jQuery.ajax({
async: false,
url: 'api.php',
type: 'POST',
data: { 'new':1 }
}).done(function(results) { });
}
function reloadOnNewVersion() {
jQuery.post('api.php', function(results) {
if (version == '')
version = results.ver;
else if (version != results.ver) {
window.onbeforeunload = null;
window.location.reload(true);
}
jQuery('#ver').html(version);
setTimeout(reloadOnNewVersion, 250);
}, 'json');
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment