当前位置: 首页IT技术 → 关于利用AJAX技术实现天气预报功能的实例教程

关于利用AJAX技术实现天气预报功能的实例教程

更多

本文章分享给大家学习的是一个ASP学习实例教程,是关于如何利用AJAX技术来实现天气预报功能的,希望对大家有用。

 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技术来实现天气预报功能 
<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数据绑定
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="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")
//转换英文为中文
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%>&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 "南京"}
热门评论
最新评论
昵称:
表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
字数: 0/500 (您的评论需要经过审核才能显示)