關(guān)于利用AJAX技術(shù)實(shí)現(xiàn)天氣預(yù)報功能的實(shí)例教程
作者: 來源: 發(fā)布時間:2011-6-1 14:37:47 點(diǎn)擊:
本文章分享給大家學(xué)習(xí)的是一個ASP學(xué)習(xí)實(shí)例教程,是關(guān)于如何利用AJAX技術(shù)來實(shí)現(xiàn)天氣預(yù)報功能的,希望對大家有用。
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技術(shù)來實(shí)現(xiàn)天氣預(yù)報功能
<A HREF=http://www.webjx.com/show.php?id=265</A>
<h3>天氣預(yù)報
<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">濟(jì)南</option>
<option value="CHXX0448">合肥</option>
<option value="CHXX0031">福州</option>
<option value="CHXX0099">南京</option>
<option value="ITXX0042">米蘭</option>
</select>
</h5>
<div id="prolist"></div>
<A HREF="http://www.webjx.com">
<img src="logo.gif" BORDER="0">
</A>
<br />
文章地址:用AJAX技術(shù)來實(shí)現(xiàn)天氣預(yù)報功能
<A HREF=http://www.webjx.com/show.php?id=265</A>
<h3>天氣預(yù)報
<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">濟(jì)南</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){
//生成隨機(jī)數(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’,’’)
//收到完整的服務(wù)器響應(yīng)
if(xmldoc.readyState == 1){
loadifo.innerHTML = "連接服務(wù)器"
}
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服務(wù)器響應(yīng)值成功
if(xmldoc.status == 200){
//將服務(wù)器返回的字符串寫到頁面中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;
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){
//生成隨機(jī)數(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’,’’)
//收到完整的服務(wù)器響應(yīng)
if(xmldoc.readyState == 1){
loadifo.innerHTML = "連接服務(wù)器"
}
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服務(wù)器響應(yīng)值成功
if(xmldoc.status == 200){
//將服務(wù)器返回的字符串寫到頁面中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;
[收藏此文章]