JavaScript 取得 Xml 文件的 LastModify
JavaScript 取得 Xml 文件的 LastModify
曾经有段时间,要是在担心,Google/Yahoo会不会不知道我的Sitemap.xml更新了,又或者Baidu不知道偶的基于BBS的开放协议生成的xml文件是否更新,虽然知道不会有这样的问题,总觉得这中间有一些东西是我所不知道的:)
直到最近鼓捣Http的时候,才发觉,除了Get/Post以外,原来还有个只关注响应首部的请求:Head请求,当服务器对Head请求做出响应时,只发送响应首部而忽略内容,由于忽略了内容,对Head请求的响应比对Get或Post的响应就小得多也快很多了.
示例如下:
JS版:
曾经有段时间,要是在担心,Google/Yahoo会不会不知道我的Sitemap.xml更新了,又或者Baidu不知道偶的基于BBS的开放协议生成的xml文件是否更新,虽然知道不会有这样的问题,总觉得这中间有一些东西是我所不知道的:)
直到最近鼓捣Http的时候,才发觉,除了Get/Post以外,原来还有个只关注响应首部的请求:Head请求,当服务器对Head请求做出响应时,只发送响应首部而忽略内容,由于忽略了内容,对Head请求的响应比对Get或Post的响应就小得多也快很多了.
示例如下:
JS版:
+展开
-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("成功响应");
}
else if(xmlhttp.status == 404) {
alert("没找到相应资源");
}
else {
alert("未知错误: " + xmlhttp.status);
}
}
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("成功响应");
}
else if(xmlhttp.status == 404) {
alert("没找到相应资源");
}
else {
alert("未知错误: " + xmlhttp.status);
}
}
加支付宝好友偷能量挖...