當前位置: 首頁IT技術 → 關于利用AJAX技術實現(xiàn)天氣預報功能的實例教程

關于利用AJAX技術實現(xiàn)天氣預報功能的實例教程

更多

本文章分享給大家學習的是一個ASP學習實例教程,是關于如何利用AJAX技術來實現(xiàn)天氣預報功能的,希望對大家有用。

 index.html

<script type="text/java script" language="java script" src="weather.js"></script>
<A HREF="http://www.webjx.com"> 
<img src="logo.gif" BORDER="0"> 
</A> 
<br />
文章地址:用AJAX技術來實現(xiàn)天氣預報功能 
<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

// java script 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數(shù)據(jù)綁定
var xmldoc = false;
function loadurl(n){
//生成隨機數(shù),同時限制刷新次數(shù)
        var now=new Date();
        var Num=now.valueOf();
//定義往址參數(shù)
        url = "weather.asp?n="+n+"&id="+Num;
//定義為異步傳輸模式
        xmldoc = false;
//Mozill,Safari等瀏覽器時需要創(chuàng)建的XMLHttp類
        if(window.XMLHttpRequest){
                xmldoc = new XMLHttpRequest();
                if(xmldoc.overrideMimeType){
                                xmldoc.overrideMimeType(’text/xml’);
                }
        }
//IE瀏覽器時創(chuàng)建的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){}
                        }
                }
        }
//不能創(chuàng)建XMLHTTP類時返回
        if(! xmldoc){
                return false;
        }
//調(diào)用CheckState函數(shù)
        xmldoc.onreadystatechange = CheckState;
        xmldoc.open(’GET’,url,true);
        xmldoc.send(null);
}
//狀態(tài)檢測
function CheckState(){
        btnSty(’loadifo’,’’)
//收到完整的服務器響應
        if(xmldoc.readyState == 1){
                loadifo.innerHTML = "連接服務器"
                }
        else if(xmldoc.readyState == 2){
                loadifo.innerHTML = "開始加載數(shù)據(jù)"
                }
        else if(xmldoc.readyState == 3){
                loadifo.innerHTML = "正在加載數(shù)據(jù)"
                settime(loadtime)
                }
        else if(xmldoc.readyState == 4){
//HTTP服務器響應值成功
                if(xmldoc.status == 200){
//將服務器返回的字符串寫到頁面中ID為showdiv的區(qū)域
     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="java script" 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")
//轉(zhuǎn)換英文為中文
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}
}
//風向函數(shù)
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 "西北風"};
}
//能見度函數(shù)
visibility=vis(visibilityV)
function vis(a){
        return a/100
}
//轉(zhuǎn)換天氣為中文
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 "晴轉(zhuǎn)云"}
        else if(obj=="AM Clouds/PM Sun/Wind"){return "云轉(zhuǎn)晴"}
        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%>&deg;</td>
  </tr>
  <tr style="background-color:#E6F5FF">
    <td align="right">實際:</td>
    <td align="center"><%=tempV%>&deg;</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%>&deg;<br />最低<%=lowV%>&deg;</td>
<%
}
%>
</tr>
</table>

 

  要想自己加天氣的話,在

<option value="CHXX0097">南昌</option>

 

  后面加相同的名稱,城市代碼到這里找http://xml.weather.yahoo.com/要想把英文城市名換成中文的,改

else if(obj=="Nanjing"){return "南京"}
熱門評論
最新評論
發(fā)表評論 查看所有評論(0)
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)