惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
Jina AI
Jina AI
I
InfoQ
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
V
Visual Studio Blog
L
LangChain Blog
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tor Project blog
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
Recorded Future
Recorded Future
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
O
OpenAI News
Google DeepMind News
Google DeepMind News
S
Schneier on Security
C
Check Point Blog
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
T
Tenable Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
Cyberwarzone
Cyberwarzone
月光博客
月光博客
The Last Watchdog
The Last Watchdog
B
Blog
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
T
Tailwind CSS Blog
Hacker News: Ask HN
Hacker News: Ask HN
H
Heimdal Security Blog
美团技术团队

博客园 - 水随风

[翱捷报表]报表系统的文件设计 [原创]从中文智能提示到表达式识别与计算 [原创]通过Attribute完成中文智能提示 2008年秋季程序员流行色之Visual studio黑色幽默 [原创]动态编译自定义代码 [资料收集]Attributes in C# [资料收集]Unicode编码问题,编程方面的技巧集合 [小记]TFS看Team编辑状态的命令行 [日语]常用日语汇总(转) [原创]Jquery下的表单验证(二) [原创]在jquery下写的表单验证 - 水随风 - 博客园 [引用]城市编码 -PHP [原创]Repeater在代码中定义模板 [原创]Html静态页面生成类 [设计模式]Memento pattern(备忘录模式)在Paint.net中的应用 [资料收集]通过 C# 简化 APM [linux]安装过程 [资料收集]All about Unsafe Code in C# [原创]数据访问类
[原创]js获取天气预报
水随风 · 2007-10-05 · via 博客园 - 水随风

2007-10-05 20:53  水随风  阅读(3272)  评论()    收藏  举报

============waterlion======js获取天气预报=========天气预报===========

看到网上的很多朋友做天气预报的时候找webservice,我也找过,不过很难找。
我呢,免得那么麻烦,懒人有懒办法。我就想直接把别人显示出来的天气请求过来用
我用JS做了如下处理

 1         var xmlhttp;
 2             function gb2utf8(data){ //编码转换
 3                var glbEncode = []; 
 4                gb2utf8_data = data; 
 5                execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript"); 
 6                var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2"); 
 7                t=t.split("@"); 
 8                var i=0,j=t.length,k; 
 9                while(++i<j) { 
10                   k=t[i].substring(0,4); 
11                   if(!glbEncode[k])  
12                   { 
13                      gb2utf8_char = eval("0x"+k); 
14                      execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript"); 
15                      glbEncode[k]=escape(gb2utf8_char).substring(1,6); 
16                   } 
17                   t[i]=glbEncode[k]+t[i].substring(4); 
18                } 
19                gb2utf8_data = gb2utf8_char = null; 
20                return unescape(t.join("%")); 
21             } 
22             function createXmlHttp()
23             {
24                 if(window.ActiveXObject)
25                 {
26                     xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
27                 }
28                 if(window.XMLHttpRequest)
29                 {
30                     xmlhttp = new XMLHttpRequest();
31                 }
32             }
33             function sendxmlHttp(weathercity)
34             {
35                 if(!weathercity)
36                 {
37                     httpstr="http://weather.tq121.com.cn/mapanel/index1.php?city=荆州"    
38                 }
39                 else
40                 {
41                     httpstr="http://weather.tq121.com.cn/mapanel/index1.php?city="+weathercity;
42                 }
43                 createXmlHttp();
44                 xmlhttp.open("GET",httpstr,true);
45                 xmlhttp.onreadystatechange=callback;
46                 xmlhttp.send(null);
47             }
48             function selectcity()
49             {
50                 sendxmlHttp(document.getElementById("txt").value);
51             }
52             function callback()
53             {
54                 if(xmlhttp.readyState==4)
55                 if(xmlhttp.status==200)
56                 {
57                     var TextDoc=gb2utf8(xmlhttp.responseBody);
58                     //var pat = /[^\x00-\xff]+/g; //这个是双字节的。就是说带标点符号
59                     var pat =/"weather">[\u4e00-\u9fa5]+<\/td>/; //这个好象带中文的。
60                     var pat1 =/"weatheren">\S*<\/td>/; //这个好象带中文的。
61                     var pat2=/<span class="big-cn">[\u4e00-\u9fa5]+<\/span>/
62                     var str="<div id="+TextDoc.match(pat);
63                     var str1="<div id="+TextDoc.match(pat1);
64                     var str2=TextDoc.match(pat2);
65                     var patt1=/td/g;
66                     document.getElementById("test").innerHTML=str2+str.replace(patt1,"div")+str1.replace(patt1,"div");        
67                 }
68             }
69        

由于我请求过来的信息中显示出来是乱码,所以我做了编码转换

这个写的是比较麻烦,但是比要找webservice好多了哈~~~

============waterlion======js获取天气预报=========天气预报===========

代码下载:weather.rar