What works:
I have an application running on a non-interactive kiosk that I need to keep updated with information that will be provided by a remote server. To do this, I've created an XMLHttpRequest/Microsoft.XMLHTTP object that checks the modified date in the header of a watched file that resides on a remote server.
This function runs on an interval, and if it detects a change in the modified date since the last check, it retrieves new information from the remote server.
The problem:
When the kiosk is unable to connect to the remote server, I want it to fall back to displaying a file stored locally. To do this, I've added a contingency to the header request so that if the response from the server was something other than 200, it would trigger the display of the fallback page.
In Safari and Firefox for Mac, and Safari and Internet Explorer for Windows, this seems to work. But I need it to work on Firefox for PC, and a 1.5 version of Firefox running on a Linux network appliance.
Where it doesn't work, nothing seems to happen. I've done tests with DIVs displaying the response on each request, and some of the browsers just seem to keep trying to connect, and failing to, but never display 0, or some other incomplete response.
In case the problem was that the OnStateChange event wasn't tripping appropriately when the connection was lost, I've added a secondary function set to call the OnStateChange method, which runs on another interval. But, this hasn't helped.
/* STATIC SETTINGS */
"use strict";
var mod_header_a = '';
var mod_header_b = '';
var mod_bool = false;
var network_err = false;
var fallback = "file:///tmp/ftproot/usb_1/fallback/alt/screen1/layout.html";
var dummy_path = "../../../dummy.txt";
var layout_path;
var rnum = '';
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var query_string = '';
function random_string()
{
query_string = '';
for (var i=0; i<string_length; i++)
{
rnum = Math.floor(Math.random() * chars.length);
query_string += chars.substring(rnum,rnum+1);
}
return query_string;
}
function reload_frame()
{
//layout_path set by inline code within index.html
document.getElementById("ajax_frame").src = layout_path+'?'+random_string();
}
function create_request_object()
{
var request_obj;
var browser = navigator.appName;
if(browser === "Microsoft Internet Explorer")
{
request_obj = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
request_obj = new XMLHttpRequest();
}
return request_obj;
}
var dum_head = create_request_object();
function determine_state()
{
if(dum_head.readyState === 4)
{
if(dum_head.status === 200)
{
if(network_err === true || mod_bool === true)
{
document.getElementById("err_div").style.display = "none";
reload_frame();
network_err = false;
}
mod_header_b = mod_header_a;
mod_header_a = dum_head.getResponseHeader("Last-Modified");
}
else
{
document.getElementById("err_div").style.display = "block";
if(network_err === false)
{
document.getElementById("ajax_frame").src = fallback+'?'+random_string();
}
network_err = true;
}
}
}
function get_header()
{
if (mod_header_a !== mod_header_b && mod_header_b !== '')
{
mod_bool = true;
}
else
{
mod_bool = false;
}
/ dum_head.open("HEAD", dummy_path+"?"+random_string(), true);
dum_head.onreadystatechange = determine_state;
dum_head.send(null);
}
window.setInterval("manual_update()", 20000);
function manual_update()
{
dum_head.onreadystatechange();
determine_state();
}