Fixed AJAX caching on IE; fixed response Content-Type; neatened JS

This commit is contained in:
rxi 2014-04-11 18:53:22 +01:00
parent 4e33d43431
commit 838e41477d

View File

@ -105,32 +105,38 @@ lovebird.page = [[
</div> </div>
</div> </div>
<script> <script>
var updateDivContent = function(id, content) {
var div = document.getElementById(id);
if (div.innerHTML != content) {
div.innerHTML = content;
return true;
}
return false;
}
/* Scroll output to bottom */ /* Scroll output to bottom */
var scrolloutput = function() { var scrolloutput = function() {
var div = document.getElementById("output"); var div = document.getElementById("output");
div.scrollTop = div.scrollHeight; div.scrollTop = div.scrollHeight;
} }
scrolloutput() scrolloutput()
/* Refresh buffer output at intervals */
/* Refresh output buffer and status */
var refresh = function() { var refresh = function() {
var req = new XMLHttpRequest(); var req = new XMLHttpRequest();
req.onreadystatechange = function() { req.onreadystatechange = function() {
if (req.readyState != 4) return; if (req.readyState != 4) return;
var status;
if (req.status == 200) { if (req.status == 200) {
status = "connected &#9679;"; updateDivContent("status", "connected &#9679;");
var div = document.getElementById("output"); if (updateDivContent("output", req.responseText)) {
if (div.innerHTML != req.responseText) {
div.innerHTML = req.responseText;
scrolloutput(); scrolloutput();
} }
} else { } else {
status = "disconnected &#9675;"; updateDivContent("status", "disconnected &#9675;");
} }
var div = document.getElementById("status");
if (div.innerHTML != status) div.innerHTML = status;
} }
req.open("GET", "/buffer", true); /* Random used to avoid IE's caching */
req.open("GET", "/buffer?" + Math.random(), true);
req.send(); req.send();
} }
setInterval(refresh, <?lua echo(lovebird.refreshrate) ?> * 1000); setInterval(refresh, <?lua echo(lovebird.refreshrate) ?> * 1000);
@ -217,9 +223,10 @@ end
function lovebird.onRequest(req, client) function lovebird.onRequest(req, client)
local head = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
-- Handle request for just the buffer -- Handle request for just the buffer
if req.url:match("buffer") then if req.url:match("buffer") then
return "HTTP/1.1 200 OK\r\n\r\n" .. lovebird.buffer return head .. lovebird.buffer
end end
-- Handle input -- Handle input
if req.body then if req.body then
@ -228,7 +235,7 @@ function lovebird.onRequest(req, client)
end end
-- Generate page -- Generate page
local t = {} local t = {}
table.insert(t, "HTTP/1.1 200 OK\r\n\r\n") table.insert(t, head)
table.insert(t, lovebird.template(lovebird.page, { lovebird = lovebird })) table.insert(t, lovebird.template(lovebird.page, { lovebird = lovebird }))
return table.concat(t) return table.concat(t)
end end