OpenVMS Source Code Demos

Homepage with AJAX-based hit counter

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>homepage with AJAX-based hit counter</title>
<script type="text/javascript">
// W3C supported properties
var nsr_domain	= document.domain;
var nsr_url	= document.URL;
var nsr_mode	= "http:\/\/";
if (nsr_url.search(/https/i)==0) nsr_mode = "https:\/\/";
</script>
<script type="text/javascript">
var	my_ajax_state = 0;
function start_ajax(msg){
	response=null;
	if (typeof XMLHttpRequest == "undefined")
	XMLHttpRequest=function(){
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0")	} catch (e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0")	} catch (e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP")	} catch (e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP")	} catch (e) {}
		throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.")
	}
	if (msg != ""){
		response=new XMLHttpRequest();
		if (response != null){
			response.onreadystatechange=ajax_event_handler;
			response.open("GET",msg,true);	// async=true
			response.send(null);		// not null for POST
		}
		my_ajax_state = 1;			// busy
	}
}
//
//	this function is called whenever something is received
//
function ajax_event_handler(){
	if (response.readyState == 4){
//
// a timer is not required for this demo
//
//		if (timeOutId1!=0){
//			clearTimeout(timeOutId1);
//			timeOutId1=0;
//		}
		if (response.status == 200){
			var resp$=response.responseText;
//			alert(resp$);
			document.getElementById("my_page_counter").innerHTML=resp$;
		}else{
//			alert("error on ajax channel:"+response.status);
		}
		my_ajax_state=0;
	}
}
</script>
<style type="text/css">
 *			{ margin:0; padding:0; border:0 }
 div.my_page_counter	{ border:0px solid black; background-color:black; padding-left: 2px; padding-right: 2px;
			  color:#00ff00; display:inline;font-weight:400; font-family: "Courier New", Courier, monospace }
</style>
</head>
<body>
<h1>homepage with AJAX-based hit counter</h1>
<div style="width:700px; text-align:center; font-weight:700">
<p>more stuff can go here</p>
<script type="text/javascript">
//
// this is the old (1995-2012) counter interface code
//
	var junk = 'Visitors: \<img src="http:\/\/'+ nsr_domain +'\/scripts\/count?df=icsis_20010101.DAT|dd=d|reload=T" alt="">';
	document.write(junk+'<br>');
//
// this is the new (2013) counter interface code employing AJAX
//
	var junk = nsr_mode + nsr_domain +'\/scripts\/nsr_counter?df=icsis_20010101.DAT';
	start_ajax(junk);
//
// if the ajax call fails then this next line will not appear
// if the ajax call doesn't return then this next line will be incomplete
//
	document.write('Visitors: <div class="my_page_counter" id="my_page_counter"></div><br>');
</script>
<p>more stuff could go here</p>
</div>
</body>
</html>