直接配合前面的後台程序,就可以實現跨域抓取天氣信息了.
目前已完美支持firefox了
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script language="JavaScript" type="text/javascript">
function $() {
return document.getElementById(arguments[0])
}
function btnSty(objId,objStyle)
{
$(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+"請重新選擇";
}
}
}
</script>
<style type="text/css">
<!--
body {
margin: 0px;
padding: 0px;
font-size: 9pt;
}
-->
</style>
</head>
<body onload="loadurl('CHXX0138')">
<div>
<select onchange="loadurl(this.value)">
<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>
<span id="loadifo"></span></div>
<div id="prolist"></div>
</body>
</html>