index.html
<script type="text/javascript" language="javascript" src="weather.js"></script>
<A HREF="http://www.webjx.com">
<img src="logo.gif" BORDER="0">
</A>
<br />
文章地址:用AJAX技術來實現天氣預報功能
<A HREF=http://www.webjx.com/show.php?id=265</A>
<h3>天氣預報
<span id="loadifo"></span>
</h3>
<h5>城市:
<select onChange="loadurl(this.value)" class="select">
<option value="CHXX0138" selected="selected">武漢</option>
<option value="CHXX0097">南昌</option>
<option value="CHXX0502">海口</option>
<option value="CHXX0008">北京</option>
<option value="CHXX0116">上海</option>
<option value="CHXX0037">廣州</option>
<option value="CHXX0259">銀川</option>
<option value="CHXX0165">鄭州</option>
<option value="CHXX0013">長沙</option>
<option value="CHXX0390">杭州</option>
<option value="CHXX0049">香港</option>
<option value="CHXX0146">西安</option>
<option value="CHXX0016">成都</option>
<option value="CHXX0110">青島</option>
<option value="CHXX0039">貴陽</option>
<option value="CHXX0064">濟南</option>
<option value="CHXX0448">合肥</option>
<option value="CHXX0031">福州</option>
<option value="CHXX0099">南京</option>
<option value="ITXX0042">米蘭</option>
</select>
</h5>
<div id="prolist"></div>
weather.js
// JavaScript Document
function btnSty(objId,objStyle)
{
document.getElementById(objId).style.display = objStyle
}
//提示視窗控制
function ifodis(){
btnSty(’loadifo’,’none’);
}
function settime(a){
setTimeout(a,1000);
}
function loadtime(){
loadifo.innerHTML += "."
}
//XML資料繫結
var xmldoc = false;
function loadurl(n){
//產生隨機數,同時限制重新整理次數
var now=new Date();
var Num=now.valueOf();
//定義往址參數
url = "weather.asp?n="+n+"&id="+Num;
//定義為非同步傳輸模式
xmldoc = false;
//Mozill,Safari等瀏覽器時需要建立的XMLHttp類
if(window.XMLHttpRequest){
xmldoc = new XMLHttpRequest();
if(xmldoc.overrideMimeType){
xmldoc.overrideMimeType(’text/xml’);
}
}
//IE瀏覽器時建立的XMLHttp類
else if(window.ActiveXObject){
try{
xmldoc = new ActiveXObject("Msxml3.XMLHTTP");
}
catch(e){
try{
xmldoc = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmldoc = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
}
//不能建立XMLHTTP類時返回
if(! xmldoc){
return false;
}
//調用CheckState函數
xmldoc.onreadystatechange = CheckState;
xmldoc.open(’GET’,url,true);
xmldoc.send(null);
}
//狀態檢測
function CheckState(){
btnSty(’loadifo’,’’)
//收到完整的伺服器響應
if(xmldoc.readyState == 1){
loadifo.innerHTML = "串連伺服器"
}
else if(xmldoc.readyState == 2){
loadifo.innerHTML = "開始載入資料"
}
else if(xmldoc.readyState == 3){
loadifo.innerHTML = "正在載入資料"
settime(loadtime)
}
else if(xmldoc.readyState == 4){
//HTTP伺服器響應值成功
if(xmldoc.status == 200){
//將伺服器返回的字串寫到頁面中ID為showdiv的地區
loadifo.innerHTML = "載入完成"
var response = xmldoc.responseText;
settime(ifodis)
prolist.innerHTML = response;
}
else{
loadifo.innerHTML = "錯誤"+xmldoc.statusText+"請重新選擇";
}
}
}
if (top.location != self.location)top.location=self.location;
weather.asp
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<%
Session.CodePage = "65001"
Response.contentType="application/xml"
Response.Expires = 0
var N=Request("n")
var Num=Request("id")
var url="http://xml.weather.yahoo.com/forecastrss?p="+N+"&u=c&id="+Num;
var xmldoc=Server.CreateObject("Microsoft.Xmlhttp");
xmldoc.open(’GET’,url,false);
xmldoc.send(null);
var response = xmldoc.responseXML.documentElement;
var lastNode=response.selectNodes
("//rss/channel/lastBuildDate");
var cityNode=response.selectNodes
("//rss/channel/yweather:location");
var windNode=response.selectNodes
("//rss/channel/yweather:wind");
var atmosphereNode=response.selectNodes
("//rss/channel/yweather:atmosphere");
var astronomyNode=response.selectNodes
("//rss/channel/yweather:astronomy");
var conditionNode=response.selectNodes
("//rss/channel/item/yweather:condition");
var forecastNode=response.selectNodes
("//rss/channel/item/yweather:forecast");
var lastV=lastNode(0).firstChild.nodeValue
//編輯屬性
function attri(a,b,c){
return a(b).attributes.getNamedItem(c).value
}
var cityV=attri(cityNode,0,"city")
var chillV=attri(windNode,0,"chill")
var directionV=attri(windNode,0,"direction")
var speedV=attri(windNode,0,"speed")
var humidityV=attri(atmosphereNode,0,"humidity")
var visibilityV=attri(atmosphereNode,0,"visibility")
var sunriseV=attri(astronomyNode,0,"sunrise")
var sunsetV=attri(astronomyNode,0,"sunset")
var textV=attri(conditionNode,0,"text")
var codeV=attri(conditionNode,0,"code")
var tempV=attri(conditionNode,0,"temp")
//轉換英文為中文
var cityCH=EN2CH(cityV)
function EN2CH(obj){
if(obj=="Wuhan"){return "武漢"}
else if(obj=="Nanchang"){return "南昌"}
else if(obj=="Haikou"){return "海口"}
else if(obj=="Beijing"){return "北京"}
else if(obj=="Shanghai"){return "上海"}
else if(obj=="Guangzhou"){return "廣州"}
else if(obj=="Yinchuan"){return "銀川"}
else if(obj=="Zhengzhou"){return "鄭州"}
else if(obj=="Changsha"){return "長沙"}
else if(obj=="Hanzhong"){return "杭州"}
else if(obj=="Hong Kong"){return "香港"}
else if(obj=="Xin’an"){return "西安"}
else if(obj=="Chengdu"){return "成都"}
else if(obj=="Qingdao"){return "青島"}
else if(obj=="Guiyang"){return "貴陽"}
else if(obj=="Jinan"){return "濟南"}
else if(obj=="Hefei"){return "合肥"}
else if(obj=="Fuzhou"){return "福州"}
else if(obj=="Nanjing"){return "南京"}
else{return obj}
}
//風向函數
var windCH=wind(directionV)
function wind(a){
if(a>337 && a<=360){return "北風"};
else if(a>=0 && a<=22){return "北風"};
else if(a>22 && a<=67){return "東北風"};
else if(a>67 && a<=112){return "東風"};
else if(a>112 && a<=157){return "東南風"};
else if(a>157 && a<=202){return "南風"};
else if(a>202 && a<=247){return "西南風"};
else if(a>247 && a<=292){return "西風"};
else if(a>292 && a<=337){return "西北風"};
}
//能見度函數
visibility=vis(visibilityV)
function vis(a){
return a/100
}
//轉換天氣為中文
var textCH=tEN2CH(textV)
function tEN2CH(obj){
if(obj=="Clear"){return "晴朗"}
else if(obj=="Sunny"){return "晴天"}
else if(obj=="Partly Cloudy"){return "局部有雲"}
else if(obj=="Mostly Clear"){return "晴"}
else if(obj=="Mostly Sunny"){return "小雲"}
else if(obj=="Mostly Cloudy"){return "多雲"}
else if(obj=="Cloudy"){return "雲"}
else if(obj=="Sunny/Wind"){return "晴轉雲"}
else if(obj=="AM Clouds/PM Sun/Wind"){return "雲轉晴"}
else if(obj=="AM Showers"){return "晨雨"}
else{return obj}
}
//天氣圖片
var weaNum=weaimg(codeV)
function weaimg(obj){
return "<img src=\"http\://us\.i1\.yimg\.com/us\.yimg.com/
i/us/we/52/"+obj+"\.gif\" />"
}
//星期替換
function dayEn(obj){
if(obj=="Sun"){return "星期曰"}
else if(obj=="Mon"){return "星期一"}
else if(obj=="Tue"){return "星期二"}
else if(obj=="Wed"){return "星期三"}
else if(obj=="Thu"){return "星期四"}
else if(obj=="Fri"){return "星期五"}
else if(obj=="Sat"){return "星期六"}
else{return obj}
}
%>
<table width="163" border="0" align="center"
cellpadding="0" cellspacing="0">
<tr>
<td rowspan="6" align="center"><%=cityCH%><br />
<%=weaNum%><br />
<%=textCH%></td>
<td align="right">感覺:</td>
<td align="center"><%=chillV%>°</td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">實際:</td>
<td align="center"><%=tempV%>°</td>
</tr>
<tr>
<td align="right">風向:</td>
<td align="center"><%=windCH%></td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">風速:</td>
<td align="center"><%=speedV%>Kph</td>
</tr>
<tr>
<td align="right">濕度:</td>
<td align="center"><%=humidityV%>%</td>
</tr>
<tr style="background-color:#E6F5FF">
<td align="right">能見度:</td>
<td align="center"><%=visibility%>km</td>
</tr>
</table>
<table width="163" border="0" align="center"
cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" align="center" class="time">
<%=lastV%></td>
</tr>
<tr>
<td bgcolor="#FFFFCC">曰出:<%=sunriseV%></td>
<td bgcolor="#FFCC00">曰落:<%=sunsetV%></td>
</tr>
</table>
<table width="163" border="0" align="center"
cellpadding="0" cellspacing="0">
<tr>
<%
for(var i=0; i<2; i++){
var dayV=attri(forecastNode,i,"day")
var dateV=attri(forecastNode,i,"date")
var lowV=attri(forecastNode,i,"low")
var highV=attri(forecastNode,i,"high")
var texttV=attri(forecastNode,i,"text")
var codetV=attri(forecastNode,i,"code")
var dayCH=dayEn(dayV)
var texttCH=tEN2CH(texttV)
var weabmp=weaimg(codetV)
//var weatNum=weaimg(codetV)
%>
<td align="center"><%=dayCH%><br /><%=weabmp%><br />
<%=texttCH%><br />最高<%=highV%>°<br />
最低<%=lowV%>°</td>
<%
}
%>
</tr>
</table>
要想自己加天氣的話,在
<option value="CHXX0097">南昌</option>
後面加相同的名稱,城市代碼到這裡找
http://xml.weather.yahoo.com/要想把英文城市名換成中文的,改
else if(obj=="Nanjing"){return "南京"}