Maison  >  Article  >  interface Web  >  用javascript为页面添加天气显示实现思路及代码_javascript技巧

用javascript为页面添加天气显示实现思路及代码_javascript技巧

WBOY
WBOYoriginal
2016-05-16 17:11:131329parcourir
复制代码 代码如下:




<script> <BR>function load(cid) <BR>{ <BR>var xmlhttp; <BR>if (window.XMLHttpRequest) <BR>{// code for IE7+, Firefox, Chrome, Opera, Safari <BR>xmlhttp=new XMLHttpRequest(); <BR>} <BR>else <BR>{// code for IE6, IE5 <BR>xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); <BR>} <BR>xmlhttp.open("GET","date.jsp?cid="+cid,false); <BR>xmlhttp.send(); <BR>var obj = eval("("+ xmlhttp.responseText+")"); <BR>//var obj=JSON.parse(xmlhttp.responseText); //IE8以上 <BR>document.getElementById("test").innerHTML=obj.weatherinfo.city+":"+obj.weatherinfo.weather1+" "+obj.weatherinfo.temp1; <BR>} <BR></script>


天气情况








date.jsp
复制代码 代码如下:


String cid = request.getParameter("cid");
URL url = new URL("http://m.weather.com.cn/data/"+cid+".html");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.connect();
InputStream cin = httpConn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(cin,"UTF-8"));
StringBuffer sb = new StringBuffer();
String rl = null;
while ((rl = reader.readLine()) != null)
sb.append(rl);
out.println(sb);
%>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn