I don’t know what’s wrong with my code, even if I change the content of the target file it still wont refresh the iframe.
<script type="text/javascript">
var page = 'data/apps/<? echo $_SESSION['name']; ?><? echo $_SESSION['last']; ?>App.html', lM;
function checkModified(){
$.get(page, function(a,a,x){
var mod = x.getResponseHeader('last-modified');
if(lM != mod){
lM = mod;
document.getElementById("frame").src += "";
}
}
}
setInterval(checkModified, 5000); // every 5 seconds
</script>
What I want to achieve is when there are changes on the target page, the iframe will automatically reload itself so that the changes can be shown to the user. Both pages are located in the same domain.
Firstly, you had a missing closing bracket “)” at the end of the
$.getmethod.The main problem, though, was probably that your server is not sending proper
Last-Modifiedheaders. The server I tested on didn’t send any, meaningmodis undefined. A workaround is to check forContent-Lengthinstead. It’s not ideal because an edited page doesn’t necessarily change size, but it seems you’re in control of the page so you could ensure you add an extra byte to force a refresh.Here is your
checkModifiedfunction updated which should work: