Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
AJAX, Browsers and HTTP Server Responses
Old 04-08-2010, 06:08 PM AJAX, Browsers and HTTP Server Responses
Junior Talker

Posts: 2
Trades: 0
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.

Code:
/* 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();
}
ibsen is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-08-2010, 06:23 PM Re: AJAX, Browsers and HTTP Server Responses
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You cannot use a XMLHTTP object in such a way, it will be a resource hog and crash the browsers (ANY browser).

Write a JAVA applet, a .NET framework application or a Standalone .exe application but what you propose will "die" after a few minutes.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-08-2010, 06:42 PM Re: AJAX, Browsers and HTTP Server Responses
Junior Talker

Posts: 2
Trades: 0
Thanks for your reply. However, I've had it run successfully for weeks, without crashing anything (and in real deployment, the appliance will be reset every night). I've done extensive testing of this code, and have fixed previous memory leaks that the current setup hasn't manifested. It's based on live update code found many places elsewhere. I'm open to suggestions for writing it better in JavaScript (e.g. however Gmail does it), but none of the alternatives you suggest will work in this application.
ibsen is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to AJAX, Browsers and HTTP Server Responses
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11573 seconds with 12 queries