<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Reading Response Headers</title>
<script type="text/javascript">
var xmlHttp;
var requestType = "";
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function doHeadRequest(request, url) {
requestType = request;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("HEAD", url, true);
xmlHttp.send(null);
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(requestType == "allResponseHeaders") {
getAllResponseHeaders(); }
else if(requestType == "lastModified") {
getLastModified();
}else if(requestType == "isResourceAvailable") {
getIsResourceAvailable();
}
}
}
function getAllResponseHeaders() {
alert(xmlHttp.getAllResponseHeaders());
}
function getLastModified() {
alert("Last Modified: " + xmlHttp.getResponseHeader("Last Modified:"));
}
function getIsResourceAvailable() {
if(xmlHttp.status == 200) {
alert("Successful response"); }
else if(xmlHttp.status == 404) {
alert("Resource is unavailable"); }
else {
alert("Unexpected response status: " + xmlHttp.status);
}
}
</script>
</head>
<body> <h1>Reading Response Headers</h1>
<a href="javascript:doHeadRequest('allResponseHeaders','readingResponseHeaders.xml');">Read All Response Headers</a> <br/>
<a href="javascript:doHeadRequest('lastModified','readingResponseHeaders.xml');">Get Last Modified Date</a> <br/>
<a href="javascript:doHeadRequest('isResourceAvailable','readingResponseHeaders.xml');">Read Available Resource</a> <br/>
<a href="javascript:doHeadRequest('isResourceAvailable','not-available.xml');">Read Unavailable Resource</a>
</body>
</html>
readingResponseHeaders.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<readingResponseHeaders>
</readingResponseHeaders>